[libc++] Remove assertions from <string_view> that are unreachable (#148598)

When assertions are enabled it is impossible to construct a
`string_view` which contains a null pointer and a non-zero size, so
assertions where we check for that on an already constructed
`string_view` are unreachable.
This commit is contained in:
Nikolas Klauser 2025-08-14 09:24:20 +02:00 committed by GitHub
parent 5b258884db
commit 7b904b09eb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 10 additions and 10 deletions

View File

@ -320,7 +320,7 @@ public:
_LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI basic_string_view(const _CharT* __s, size_type __len) _NOEXCEPT _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI basic_string_view(const _CharT* __s, size_type __len) _NOEXCEPT
_LIBCPP_DIAGNOSE_NULLPTR_IF(__len != 0 && __s == nullptr, " if len is not zero") _LIBCPP_DIAGNOSE_NULLPTR_IF(__len != 0 && __s == nullptr, " if len is not zero")
: __data_(__s), __size_(__len) { : __data_(__s), __size_(__len) {
# if _LIBCPP_STD_VER >= 14 # if !defined(_LIBCPP_CXX03_LANG) && (!defined(_LIBCPP_COMPILER_GCC) || _LIBCPP_STD_VER >= 14)
// Allocations must fit in `ptrdiff_t` for pointer arithmetic to work. If `__len` exceeds it, the input // Allocations must fit in `ptrdiff_t` for pointer arithmetic to work. If `__len` exceeds it, the input
// range could not have been valid. Most likely the caller underflowed some arithmetic and inadvertently // range could not have been valid. Most likely the caller underflowed some arithmetic and inadvertently
// passed in a negative length. // passed in a negative length.
@ -502,7 +502,6 @@ public:
// find // find
_LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
find(basic_string_view __s, size_type __pos = 0) const _NOEXCEPT { find(basic_string_view __s, size_type __pos = 0) const _NOEXCEPT {
_LIBCPP_ASSERT_NON_NULL(__s.size() == 0 || __s.data() != nullptr, "string_view::find(): received nullptr");
return std::__str_find<value_type, size_type, traits_type, npos>(data(), size(), __s.data(), __pos, __s.size()); return std::__str_find<value_type, size_type, traits_type, npos>(data(), size(), __s.data(), __pos, __s.size());
} }
@ -527,7 +526,6 @@ public:
// rfind // rfind
_LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
rfind(basic_string_view __s, size_type __pos = npos) const _NOEXCEPT { rfind(basic_string_view __s, size_type __pos = npos) const _NOEXCEPT {
_LIBCPP_ASSERT_NON_NULL(__s.size() == 0 || __s.data() != nullptr, "string_view::find(): received nullptr");
return std::__str_rfind<value_type, size_type, traits_type, npos>(data(), size(), __s.data(), __pos, __s.size()); return std::__str_rfind<value_type, size_type, traits_type, npos>(data(), size(), __s.data(), __pos, __s.size());
} }
@ -553,7 +551,6 @@ public:
// find_first_of // find_first_of
_LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
find_first_of(basic_string_view __s, size_type __pos = 0) const _NOEXCEPT { find_first_of(basic_string_view __s, size_type __pos = 0) const _NOEXCEPT {
_LIBCPP_ASSERT_NON_NULL(__s.size() == 0 || __s.data() != nullptr, "string_view::find_first_of(): received nullptr");
return std::__str_find_first_of<value_type, size_type, traits_type, npos>( return std::__str_find_first_of<value_type, size_type, traits_type, npos>(
data(), size(), __s.data(), __pos, __s.size()); data(), size(), __s.data(), __pos, __s.size());
} }
@ -580,7 +577,6 @@ public:
// find_last_of // find_last_of
_LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
find_last_of(basic_string_view __s, size_type __pos = npos) const _NOEXCEPT { find_last_of(basic_string_view __s, size_type __pos = npos) const _NOEXCEPT {
_LIBCPP_ASSERT_NON_NULL(__s.size() == 0 || __s.data() != nullptr, "string_view::find_last_of(): received nullptr");
return std::__str_find_last_of<value_type, size_type, traits_type, npos>( return std::__str_find_last_of<value_type, size_type, traits_type, npos>(
data(), size(), __s.data(), __pos, __s.size()); data(), size(), __s.data(), __pos, __s.size());
} }
@ -607,8 +603,6 @@ public:
// find_first_not_of // find_first_not_of
_LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
find_first_not_of(basic_string_view __s, size_type __pos = 0) const _NOEXCEPT { find_first_not_of(basic_string_view __s, size_type __pos = 0) const _NOEXCEPT {
_LIBCPP_ASSERT_NON_NULL(
__s.size() == 0 || __s.data() != nullptr, "string_view::find_first_not_of(): received nullptr");
return std::__str_find_first_not_of<value_type, size_type, traits_type, npos>( return std::__str_find_first_not_of<value_type, size_type, traits_type, npos>(
data(), size(), __s.data(), __pos, __s.size()); data(), size(), __s.data(), __pos, __s.size());
} }
@ -635,8 +629,6 @@ public:
// find_last_not_of // find_last_not_of
_LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
find_last_not_of(basic_string_view __s, size_type __pos = npos) const _NOEXCEPT { find_last_not_of(basic_string_view __s, size_type __pos = npos) const _NOEXCEPT {
_LIBCPP_ASSERT_NON_NULL(
__s.size() == 0 || __s.data() != nullptr, "string_view::find_last_not_of(): received nullptr");
return std::__str_find_last_not_of<value_type, size_type, traits_type, npos>( return std::__str_find_last_not_of<value_type, size_type, traits_type, npos>(
data(), size(), __s.data(), __pos, __s.size()); data(), size(), __s.data(), __pos, __s.size());
} }

View File

@ -7,7 +7,8 @@
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
// REQUIRES: has-unix-headers // REQUIRES: has-unix-headers
// UNSUPPORTED: c++03, c++11 // UNSUPPORTED: c++03
// UNSUPPORTED: c++11 && gcc
// REQUIRES: libcpp-hardening-mode={{extensive|debug}} // REQUIRES: libcpp-hardening-mode={{extensive|debug}}
// XFAIL: libcpp-hardening-mode=debug && availability-verbose_abort-missing // XFAIL: libcpp-hardening-mode=debug && availability-verbose_abort-missing
@ -17,10 +18,17 @@
#include <string_view> #include <string_view>
#include "check_assertion.h" #include "check_assertion.h"
#include "test_macros.h"
// We're testing for assertions here, so let's not diagnose the misuses at compile time
// FIXME: This should really be in ADDITIONAL_COMPILE_FLAGS, but it that doesn't work due to a Clang bug
TEST_CLANG_DIAGNOSTIC_IGNORED("-Wnonnull")
int main(int, char**) { int main(int, char**) {
char c = 0; char c = 0;
TEST_LIBCPP_ASSERT_FAILURE( TEST_LIBCPP_ASSERT_FAILURE(
std::string_view(&c, -1), "string_view::string_view(_CharT *, size_t): length does not fit in difference_type"); std::string_view(&c, -1), "string_view::string_view(_CharT *, size_t): length does not fit in difference_type");
TEST_LIBCPP_ASSERT_FAILURE(
std::string_view(nullptr, 1), "string_view::string_view(_CharT *, size_t): received nullptr");
return 0; return 0;
} }