From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id 4D1C13858410; Thu, 2 Nov 2023 14:54:01 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4D1C13858410 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1698936841; bh=PNHOIligDALNNpLuqnO+9uW33JrvJ43dViu+aS6Gusw=; h=From:To:Subject:Date:From; b=Zjx2W/4EE/X7L02ch/qACVB/J2z27XTBH0fe+WQ0GV1RCrx5VGIV425FCPIa/aPKI oa26hssasKfNu0q5uB+Fw+8uvWp2QnITD2gyvBMpzMxiSUMlaqoL/5PbWfLVw11kdG SHf//gRtwu6bACXpl1dKIUphLR4nXD9qJTkhQIMI= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jonathan Wakely To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r14-5085] libstdc++: Add assertion to std::string_view::remove_suffix [PR112314] X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/master X-Git-Oldrev: f432a594fe6d3a0de1330ba69200d158e6248083 X-Git-Newrev: 6afa984f47e16e8bd958646d7407b74e61041f5d Message-Id: <20231102145401.4D1C13858410@sourceware.org> Date: Thu, 2 Nov 2023 14:54:01 +0000 (GMT) List-Id: https://gcc.gnu.org/g:6afa984f47e16e8bd958646d7407b74e61041f5d commit r14-5085-g6afa984f47e16e8bd958646d7407b74e61041f5d Author: Jonathan Wakely Date: Wed Nov 1 15:01:22 2023 +0000 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. Diff: --- libstdc++-v3/include/std/string_view | 5 ++++- .../basic_string_view/modifiers/remove_prefix/debug.cc | 14 ++++++++++++++ .../basic_string_view/modifiers/remove_suffix/debug.cc | 14 ++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/libstdc++-v3/include/std/string_view b/libstdc++-v3/include/std/string_view index d103abda6688..9deae25f7120 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 000000000000..37204583b716 --- /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 + +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 000000000000..a549e4c2471b --- /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 + +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();