From patchwork Mon Oct 11 19:37:29 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 46110 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 7E44D3858437 for ; Mon, 11 Oct 2021 19:41:14 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7E44D3858437 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1633981274; bh=LiVg7mFh1ZPJheYeHgJZms2m3wJrUG9m1YNcN6nHFxk=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=N0wsusLd/qXVmXRUnbyAWZmuZHJolUJRtxSZF26rjGyNNkDR5s4BZs6gtqG8mV2e+ 2/VdCMkAA2THqXVo5J4Q+ER14b0070qADbGJIXqy1aPC0KaO0eaMYuuTcQUkBYjJZv GSqlBOwAj4E7Gm4VpGZ8IgJdYbsJzMIu3/Qu5tqI= 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 ESMTP id 48D123857C50 for ; Mon, 11 Oct 2021 19:37:37 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 48D123857C50 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-380-QwC8qS_lOUiI4kNllV3EDQ-1; Mon, 11 Oct 2021 15:37:32 -0400 X-MC-Unique: QwC8qS_lOUiI4kNllV3EDQ-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id C6C8C801A92; Mon, 11 Oct 2021 19:37:30 +0000 (UTC) Received: from localhost (unknown [10.33.37.44]) by smtp.corp.redhat.com (Postfix) with ESMTP id 726DC5C232; Mon, 11 Oct 2021 19:37:30 +0000 (UTC) Date: Mon, 11 Oct 2021 20:37:29 +0100 To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Add valid range assertions to std::basic_regex [PR89927] Message-ID: MIME-Version: 1.0 X-Clacks-Overhead: GNU Terry Pratchett X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-14.5 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_LOW, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=unavailable autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jonathan Wakely via Gcc-patches From: Jonathan Wakely Reply-To: Jonathan Wakely Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" This adds some debug assertions to basic_regex. They don't actually diagnose the error in the PR yet, but I have another patch to make them more effective. Also change the __glibcxx_assert(false) consistency checks to include a string literal that tells the user a bit more about why the process aborted. We could consider adding a __glibcxx_bug or __glibcxx_internal_error macro for this purpose, but ideally we'll never hit such bugs anyway so it shouldn't be needed. libstdc++-v3/ChangeLog: PR libstdc++/89927 * include/bits/regex.h (basic_regex(const _Ch_type*, size_t)): Add __glibcxx_requires_string_len assertion. (basic_regex::assign(InputIterator, InputIterator)): Add __glibcxx_requires_valid_range assertion. * include/bits/regex_scanner.tcc (_Scanner::_M_advance()) (_Scanner::_M_scan_normal()): Use string literal in assertions. Tested powerpc64le-linux. Committed to trunk. commit 6b6788f8c2748060d922cc22173ff7f8500917e9 Author: Jonathan Wakely Date: Mon Oct 11 12:08:59 2021 libstdc++: Add valid range assertions to std::basic_regex [PR89927] This adds some debug assertions to basic_regex. They don't actually diagnose the error in the PR yet, but I have another patch to make them more effective. Also change the __glibcxx_assert(false) consistency checks to include a string literal that tells the user a bit more about why the process aborted. We could consider adding a __glibcxx_bug or __glibcxx_internal_error macro for this purpose, but ideally we'll never hit such bugs anyway so it shouldn't be needed. libstdc++-v3/ChangeLog: PR libstdc++/89927 * include/bits/regex.h (basic_regex(const _Ch_type*, size_t)): Add __glibcxx_requires_string_len assertion. (basic_regex::assign(InputIterator, InputIterator)): Add __glibcxx_requires_valid_range assertion. * include/bits/regex_scanner.tcc (_Scanner::_M_advance()) (_Scanner::_M_scan_normal()): Use string literal in assertions. diff --git a/libstdc++-v3/include/bits/regex.h b/libstdc++-v3/include/bits/regex.h index bf02bff7c49..3c44bcd7e33 100644 --- a/libstdc++-v3/include/bits/regex.h +++ b/libstdc++-v3/include/bits/regex.h @@ -467,7 +467,10 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 */ basic_regex(const _Ch_type* __p, std::size_t __len, flag_type __f = ECMAScript) - { _M_compile(__p, __p + __len, __f); } + { + __glibcxx_requires_string_len(__p, __len); + _M_compile(__p, __p + __len, __f); + } /** * @brief Copy-constructs a basic regular expression. @@ -684,6 +687,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 if constexpr (__detail::__is_contiguous_iter<_InputIterator>::value && is_same_v<_ValT, value_type>) { + __glibcxx_requires_valid_range(__first, __last); const auto __len = __last - __first; const _Ch_type* __p = std::__to_address(__first); _M_compile(__p, __p + __len, __flags); diff --git a/libstdc++-v3/include/bits/regex_scanner.tcc b/libstdc++-v3/include/bits/regex_scanner.tcc index d81627dc3e9..2fa2303f27b 100644 --- a/libstdc++-v3/include/bits/regex_scanner.tcc +++ b/libstdc++-v3/include/bits/regex_scanner.tcc @@ -83,7 +83,7 @@ namespace __detail _M_scan_in_brace(); else { - __glibcxx_assert(false); + __glibcxx_assert(!"unexpected state while processing regex"); } } @@ -195,7 +195,7 @@ namespace __detail _M_token = __it->second; return; } - __glibcxx_assert(false); + __glibcxx_assert(!"unexpected special character in regex"); } else {