Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 1 addition & 9 deletions libcxx/include/string_view
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ public:
_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")
: __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
// range could not have been valid. Most likely the caller underflowed some arithmetic and inadvertently
// passed in a negative length.
Expand Down Expand Up @@ -502,7 +502,6 @@ public:
// find
_LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
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());
}

Expand All @@ -527,7 +526,6 @@ public:
// rfind
_LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
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());
}

Expand All @@ -553,7 +551,6 @@ public:
// find_first_of
_LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
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>(
data(), size(), __s.data(), __pos, __s.size());
}
Expand All @@ -580,7 +577,6 @@ public:
// find_last_of
_LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
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>(
data(), size(), __s.data(), __pos, __s.size());
}
Expand All @@ -607,8 +603,6 @@ public:
// find_first_not_of
_LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
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>(
data(), size(), __s.data(), __pos, __s.size());
}
Expand All @@ -635,8 +629,6 @@ public:
// find_last_not_of
_LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
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>(
data(), size(), __s.data(), __pos, __s.size());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
//===----------------------------------------------------------------------===//

// REQUIRES: has-unix-headers
// UNSUPPORTED: c++03, c++11
// UNSUPPORTED: c++03
// UNSUPPORTED: c++11 && gcc
// REQUIRES: libcpp-hardening-mode={{extensive|debug}}
// XFAIL: libcpp-hardening-mode=debug && availability-verbose_abort-missing

Expand All @@ -17,10 +18,17 @@
#include <string_view>

#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**) {
char c = 0;
TEST_LIBCPP_ASSERT_FAILURE(
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;
}
Loading