From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 44ABF3858C60 for ; Thu, 2 Nov 2023 14:54:39 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 44ABF3858C60 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 44ABF3858C60 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698936887; cv=none; b=QyClyr6uh+sewaXBJx18d5X/sZHvyJE1wPNNeVf6INtIgBarP8sx1V9vxOaH19vF0+sgWlYmivsm90UaeSqR/J8PXLF+gx9JdwY6JN4Etdxe/4AB91xztanE2YCeKVXy6FqxdhMDLSG6EsHDXyJEnmx1EhwO1Y6R07IOgWm2BBQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698936887; c=relaxed/simple; bh=F3NwLkIhBrd2bQbRr5qO+YiQw8wDMobRykZ0/oyfXoQ=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=OJsnxqVqtqBcLuVH1FBaMko+hzul3kdasZktTaceULwaLZhZCWcJu+dFcTDJpvrJQTnRzpkb0MLynicOUK5zGuvBhuAWMwKpMN/mxjYebzfCn3pqJ+55gm+sjGGpqSxBaUs50kudF6zp/kjXr6EiOdjx1NF40NwDTDSFJuLPLLY= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1698936879; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=4YmzNLDY6pzeZE1kbpTZOMI9vTTKJsbcjZxhasK2TSw=; b=DQnu+7+Lml0R8J54PEHCya4j/VQG3sPpYTF1ysLUPF7vb6SHo1tpAd8kv4mLYGyox0FPIF dfPW/XfKcKSYH7kdZiB+DhjAh0k2D/wgfvfmtbFxjeEEm5zSkbhoqNinN1z//68IZI2beM tDBzNgpoY3dF0hRLPWdJQmImJuQH6xs= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-592-F9Y_CE9SOA6c2TRDBPNd2Q-1; Thu, 02 Nov 2023 10:54:35 -0400 X-MC-Unique: F9Y_CE9SOA6c2TRDBPNd2Q-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 885303810B1F; Thu, 2 Nov 2023 14:54:35 +0000 (UTC) Received: from localhost (unknown [10.42.28.65]) by smtp.corp.redhat.com (Postfix) with ESMTP id 22A7D502D; Thu, 2 Nov 2023 14:54:34 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Add assertion to std::string_view::remove_suffix [PR112314] Date: Thu, 2 Nov 2023 14:54:01 +0000 Message-ID: <20231102145434.2812083-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.5 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-12.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: Tested x86_64-linux. Pushed to trunk. Backports seem reasonable. -- >8 -- 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 | 5 ++++- .../modifiers/remove_prefix/debug.cc | 14 ++++++++++++++ .../modifiers/remove_suffix/debug.cc | 14 ++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 libstdc++-v3/testsuite/21_strings/basic_string_view/modifiers/remove_prefix/debug.cc create mode 100644 libstdc++-v3/testsuite/21_strings/basic_string_view/modifiers/remove_suffix/debug.cc diff --git a/libstdc++-v3/include/std/string_view b/libstdc++-v3/include/std/string_view index d103abda668..9deae25f712 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 00000000000..37204583b71 --- /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 00000000000..a549e4c2471 --- /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(); -- 2.41.0