Skip to content

Commit 7b904b0

Browse files
authored
[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.
1 parent 5b25888 commit 7b904b0

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

libcxx/include/string_view

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ public:
320320
_LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI basic_string_view(const _CharT* __s, size_type __len) _NOEXCEPT
321321
_LIBCPP_DIAGNOSE_NULLPTR_IF(__len != 0 && __s == nullptr, " if len is not zero")
322322
: __data_(__s), __size_(__len) {
323-
# if _LIBCPP_STD_VER >= 14
323+
# if !defined(_LIBCPP_CXX03_LANG) && (!defined(_LIBCPP_COMPILER_GCC) || _LIBCPP_STD_VER >= 14)
324324
// Allocations must fit in `ptrdiff_t` for pointer arithmetic to work. If `__len` exceeds it, the input
325325
// range could not have been valid. Most likely the caller underflowed some arithmetic and inadvertently
326326
// passed in a negative length.
@@ -502,7 +502,6 @@ public:
502502
// find
503503
_LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
504504
find(basic_string_view __s, size_type __pos = 0) const _NOEXCEPT {
505-
_LIBCPP_ASSERT_NON_NULL(__s.size() == 0 || __s.data() != nullptr, "string_view::find(): received nullptr");
506505
return std::__str_find<value_type, size_type, traits_type, npos>(data(), size(), __s.data(), __pos, __s.size());
507506
}
508507

@@ -527,7 +526,6 @@ public:
527526
// rfind
528527
_LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
529528
rfind(basic_string_view __s, size_type __pos = npos) const _NOEXCEPT {
530-
_LIBCPP_ASSERT_NON_NULL(__s.size() == 0 || __s.data() != nullptr, "string_view::find(): received nullptr");
531529
return std::__str_rfind<value_type, size_type, traits_type, npos>(data(), size(), __s.data(), __pos, __s.size());
532530
}
533531

@@ -553,7 +551,6 @@ public:
553551
// find_first_of
554552
_LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
555553
find_first_of(basic_string_view __s, size_type __pos = 0) const _NOEXCEPT {
556-
_LIBCPP_ASSERT_NON_NULL(__s.size() == 0 || __s.data() != nullptr, "string_view::find_first_of(): received nullptr");
557554
return std::__str_find_first_of<value_type, size_type, traits_type, npos>(
558555
data(), size(), __s.data(), __pos, __s.size());
559556
}
@@ -580,7 +577,6 @@ public:
580577
// find_last_of
581578
_LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
582579
find_last_of(basic_string_view __s, size_type __pos = npos) const _NOEXCEPT {
583-
_LIBCPP_ASSERT_NON_NULL(__s.size() == 0 || __s.data() != nullptr, "string_view::find_last_of(): received nullptr");
584580
return std::__str_find_last_of<value_type, size_type, traits_type, npos>(
585581
data(), size(), __s.data(), __pos, __s.size());
586582
}
@@ -607,8 +603,6 @@ public:
607603
// find_first_not_of
608604
_LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
609605
find_first_not_of(basic_string_view __s, size_type __pos = 0) const _NOEXCEPT {
610-
_LIBCPP_ASSERT_NON_NULL(
611-
__s.size() == 0 || __s.data() != nullptr, "string_view::find_first_not_of(): received nullptr");
612606
return std::__str_find_first_not_of<value_type, size_type, traits_type, npos>(
613607
data(), size(), __s.data(), __pos, __s.size());
614608
}
@@ -635,8 +629,6 @@ public:
635629
// find_last_not_of
636630
_LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
637631
find_last_not_of(basic_string_view __s, size_type __pos = npos) const _NOEXCEPT {
638-
_LIBCPP_ASSERT_NON_NULL(
639-
__s.size() == 0 || __s.data() != nullptr, "string_view::find_last_not_of(): received nullptr");
640632
return std::__str_find_last_not_of<value_type, size_type, traits_type, npos>(
641633
data(), size(), __s.data(), __pos, __s.size());
642634
}

libcxx/test/libcxx/strings/string.view/assert.ctor.length.pass.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
//===----------------------------------------------------------------------===//
88

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

@@ -17,10 +18,17 @@
1718
#include <string_view>
1819

1920
#include "check_assertion.h"
21+
#include "test_macros.h"
22+
23+
// We're testing for assertions here, so let's not diagnose the misuses at compile time
24+
// FIXME: This should really be in ADDITIONAL_COMPILE_FLAGS, but it that doesn't work due to a Clang bug
25+
TEST_CLANG_DIAGNOSTIC_IGNORED("-Wnonnull")
2026

2127
int main(int, char**) {
2228
char c = 0;
2329
TEST_LIBCPP_ASSERT_FAILURE(
2430
std::string_view(&c, -1), "string_view::string_view(_CharT *, size_t): length does not fit in difference_type");
31+
TEST_LIBCPP_ASSERT_FAILURE(
32+
std::string_view(nullptr, 1), "string_view::string_view(_CharT *, size_t): received nullptr");
2533
return 0;
2634
}

0 commit comments

Comments
 (0)