diff options
author | Jonathan Wakely <jwakely@redhat.com> | 2023-11-01 15:01:22 +0000 |
---|---|---|
committer | Jonathan Wakely <jwakely@redhat.com> | 2023-11-02 14:53:23 +0000 |
commit | 6afa984f47e16e8bd958646d7407b74e61041f5d (patch) | |
tree | 5de71a34701f92fd653db08eda2df6f88f0e47c1 | |
parent | f432a594fe6d3a0de1330ba69200d158e6248083 (diff) | |
download | gcc-6afa984f47e16e8bd958646d7407b74e61041f5d.zip gcc-6afa984f47e16e8bd958646d7407b74e61041f5d.tar.gz gcc-6afa984f47e16e8bd958646d7407b74e61041f5d.tar.bz2 |
libstdc++: Add assertion to std::string_view::remove_suffix [PR112314]
libstdc++-v3/ChangeLog:
PR libstdc++/112314
* include/std/string_view (string_view::remove_suffix): Add
debug assertion.
* testsuite/21_strings/basic_string_view/modifiers/remove_prefix/debug.cc:
New test.
* testsuite/21_strings/basic_string_view/modifiers/remove_suffix/debug.cc:
New test.
3 files changed, 32 insertions, 1 deletions
diff --git a/libstdc++-v3/include/std/string_view b/libstdc++-v3/include/std/string_view index d103abd..9deae25 100644 --- a/libstdc++-v3/include/std/string_view +++ b/libstdc++-v3/include/std/string_view @@ -301,7 +301,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION constexpr void remove_suffix(size_type __n) noexcept - { this->_M_len -= __n; } + { + __glibcxx_assert(this->_M_len >= __n); + this->_M_len -= __n; + } constexpr void swap(basic_string_view& __sv) noexcept diff --git a/libstdc++-v3/testsuite/21_strings/basic_string_view/modifiers/remove_prefix/debug.cc b/libstdc++-v3/testsuite/21_strings/basic_string_view/modifiers/remove_prefix/debug.cc new file mode 100644 index 0000000..3720458 --- /dev/null +++ b/libstdc++-v3/testsuite/21_strings/basic_string_view/modifiers/remove_prefix/debug.cc @@ -0,0 +1,14 @@ +// { dg-do compile { target c++17 } } + +#include <string_view> + +constexpr bool +check_remove_prefix() +{ + std::string_view sv("123"); + sv.remove_prefix(4); + // { dg-error "not a constant expression" "" { target *-*-* } 0 } + return true; +} + +constexpr bool test = check_remove_prefix(); diff --git a/libstdc++-v3/testsuite/21_strings/basic_string_view/modifiers/remove_suffix/debug.cc b/libstdc++-v3/testsuite/21_strings/basic_string_view/modifiers/remove_suffix/debug.cc new file mode 100644 index 0000000..a549e4c --- /dev/null +++ b/libstdc++-v3/testsuite/21_strings/basic_string_view/modifiers/remove_suffix/debug.cc @@ -0,0 +1,14 @@ +// { dg-do compile { target c++17 } } + +#include <string_view> + +constexpr bool +check_remove_suffix() +{ + std::string_view sv("123"); + sv.remove_suffix(4); + // { dg-error "not a constant expression" "" { target *-*-* } 0 } + return true; +} + +constexpr bool test = check_remove_suffix(); |