From 34b5c10b5749310b207e86c641b03149cf0a896e Mon Sep 17 00:00:00 2001 From: Andrei Elovikov Date: Mon, 25 Aug 2025 10:39:32 -0700 Subject: [PATCH] [SYCL] Update `detail::string_view` impl * Fix potential `nullptr` dereference when casting default-constructed object to `std::string_view`. * Make that conversion operator implicit (because why shouldn't it be?) * Not that it's implicit, there is no need in comparison operators with `std::string_view`, because that conversion will be considered implicitly. I also have no idea why there was `__INTEL_PREVIEW_BREAKING_CHANGES` guard before. --- sycl/include/sycl/detail/string_view.hpp | 42 ++---------------------- 1 file changed, 3 insertions(+), 39 deletions(-) diff --git a/sycl/include/sycl/detail/string_view.hpp b/sycl/include/sycl/detail/string_view.hpp index a4e1c0bf20436..d394de736d847 100644 --- a/sycl/include/sycl/detail/string_view.hpp +++ b/sycl/include/sycl/detail/string_view.hpp @@ -68,51 +68,15 @@ class string_view { constexpr const char *data() const noexcept { return str ? str : ""; } - constexpr explicit operator std::string_view() const noexcept { + constexpr operator std::string_view() const noexcept { + if (str == nullptr) + return std::string_view{}; #ifdef __INTEL_PREVIEW_BREAKING_CHANGES return std::string_view(str, len); #else return std::string_view(str); #endif } - -#ifdef __INTEL_PREVIEW_BREAKING_CHANGES - friend constexpr bool operator==(string_view lhs, - std::string_view rhs) noexcept { - return rhs == std::string_view(lhs); - } - friend constexpr bool operator==(std::string_view lhs, - string_view rhs) noexcept { - return lhs == std::string_view(rhs); - } - - friend constexpr bool operator!=(string_view lhs, - std::string_view rhs) noexcept { - return rhs != std::string_view(lhs); - } - friend constexpr bool operator!=(std::string_view lhs, - string_view rhs) noexcept { - return lhs != std::string_view(rhs); - } -#else - friend constexpr bool operator==(string_view lhs, - std::string_view rhs) noexcept { - return rhs == lhs.data(); - } - friend constexpr bool operator==(std::string_view lhs, - string_view rhs) noexcept { - return lhs == rhs.data(); - } - - friend constexpr bool operator!=(string_view lhs, - std::string_view rhs) noexcept { - return rhs != lhs.data(); - } - friend constexpr bool operator!=(std::string_view lhs, - string_view rhs) noexcept { - return lhs != rhs.data(); - } -#endif }; } // namespace detail