]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Add assertion to std::string_view::remove_suffix [PR112314]
authorJonathan Wakely <jwakely@redhat.com>
Wed, 1 Nov 2023 15:01:22 +0000 (15:01 +0000)
committerJonathan Wakely <jwakely@redhat.com>
Thu, 2 Nov 2023 14:53:23 +0000 (14:53 +0000)
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.

libstdc++-v3/include/std/string_view
libstdc++-v3/testsuite/21_strings/basic_string_view/modifiers/remove_prefix/debug.cc [new file with mode: 0644]
libstdc++-v3/testsuite/21_strings/basic_string_view/modifiers/remove_suffix/debug.cc [new file with mode: 0644]

index d103abda66886cc898f8d00337ceba6a995ff4dc..9deae25f71209513dab605a4ca5a512fa11c51b1 100644 (file)
@@ -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 (file)
index 0000000..3720458
--- /dev/null
@@ -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 (file)
index 0000000..a549e4c
--- /dev/null
@@ -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();