From patchwork Thu Nov 2 14:54:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 160971 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:8f47:0:b0:403:3b70:6f57 with SMTP id j7csp454487vqu; Thu, 2 Nov 2023 08:58:20 -0700 (PDT) X-Google-Smtp-Source: AGHT+IH8F1k6ZFByv7aNw4nBnV8XrxT8vYXFjEMuLnaE7XTzLU1h4ecznGu284J14dn7Kyog7B6N X-Received: by 2002:a05:622a:1054:b0:400:9f2c:1211 with SMTP id f20-20020a05622a105400b004009f2c1211mr310759qte.29.1698940700490; Thu, 02 Nov 2023 08:58:20 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1698940700; cv=pass; d=google.com; s=arc-20160816; b=Kskqc7D9Ovfmn7sHM345IfRRJs7mUD2/MJnFt0dIwHpDY3XFH9+SqZIP4KSZ7woMe8 em205zQwc8Q6GOgddg/w0/XQaZSTWk4Wg4nwmHX+bOK9Tv4dnPoNtyXab5nIJRygXJBX KNIYaf65q6gBie1rmnnQxmWH2QldnbeavkxmK8Pn6e5LHKrnVlun59ITdW1szWAOwSme OYF0K8PCIUnprbWSA5LDlkQSbQBC/i27di0i/5nPgd/iY2eifePnkzPPApVkEE02P0KN vtdjdCdV1dA8e1VoubuxNg/9B3KuXIBCtrFotDKOfi8+Trk8+N/Hy6yOc+wNlAMiIPuW Gl3g== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:to:from:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=4YmzNLDY6pzeZE1kbpTZOMI9vTTKJsbcjZxhasK2TSw=; fh=sJ+2/4g29YdyXkoRrFZSpsL2zxijepB7X/1rB0LDDh8=; b=S9prS3rAMvuIf5xV/+nvtgzr5JcnAFwkpj/YxT5cIvICxMKZYEyMZa8fOnJbj61HRU 1EMPIj0n7jkSWZT749G7VE5nQ+3V6n2l0VpzUvvyYcvnMYKVMok1iiHG1kOlIq6BHKXA JdpE1Gryw5i6xyOaBvFTLtZLPCTACKu8zeWOriGMkZB+oK5WnnBjY70de+oaLfz9pQPG sKtCEHx74ZnWc4hds+6PwEq6/nQHRTt87cBSMt0dLD9skOC8iwGa/t5IgFRS49FEpEoX hp1dInzw03anlaUFG/S7mW8r4ltkv0J98y0nnnr3JQyw7kwTCW0x100H9SJdEWt9b7rm 2kHw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=daAQwPUS; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id j6-20020ac85c46000000b00417ccac55acsi100978qtj.641.2023.11.02.08.58.20 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Nov 2023 08:58:20 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=daAQwPUS; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 0A8A63856DF4 for ; Thu, 2 Nov 2023 14:55:10 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org 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 99CE93858D39 for ; Thu, 2 Nov 2023 14:54:37 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 99CE93858D39 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 99CE93858D39 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=iYs5ghpUh4uNhB5RmxeB1ptv8J3n+xFmTYg8oetMhHCeAa6BM3wkBQ4uI7EZiiIVchGp8n5pClOPMKKxt4EpJxBdKVSl2EIknNiltY69VjdLzyXWnkM5Z2JBNgtOcUbFGxwvRMOs3wH8+m2gDXWSOsFYlEHlVKdMkQeXxVkB1/I= 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=FYFAVXw9ydLprJURg/9OGfF6VBjEquqC9NlwDYmrm+dwy5fgKS9lpzWpBHtmR3Vxl5Onu4CHkH68mZZ7sK9c+SrCQr+74XgO1kteZ11AhidfHvAex0/zXPWHHh6hd73OGXeDs7a41ntzow526ezdPENQSWui/PEq4LsrMvwGkO0= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1698936877; 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=daAQwPUSlysy+of++JYXqMz7KpLHlwfFwOyMh01pj7T2duhuV3WN0tsFjCBPY3QrQhtr13 XOjvkDiz/+IgQV2e1V92TeChKQRpTijAFcJHcFN3DZeXSHoV3yG4qPJoz6FnE9/keAt5QJ ccaJGqTVApq2AMdntC1i/7nOpZGi9oo= 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 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 X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1781468443619855908 X-GMAIL-MSGID: 1781468443619855908 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();