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 ESMTP id 9CB7D3857C4A for ; Mon, 11 Oct 2021 19:37:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 9CB7D3857C4A 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-156-U-HU5fgjNSylZiPbPNicYw-1; Mon, 11 Oct 2021 15:37:25 -0400 X-MC-Unique: U-HU5fgjNSylZiPbPNicYw-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id CA08E100C660; Mon, 11 Oct 2021 19:37:24 +0000 (UTC) Received: from localhost (unknown [10.33.37.44]) by smtp.corp.redhat.com (Postfix) with ESMTP id 3FED119E7E; Mon, 11 Oct 2021 19:37:24 +0000 (UTC) Date: Mon, 11 Oct 2021 20:37:23 +0100 From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Fix std::match_results::end() for failed matches [PR102667] Message-ID: MIME-Version: 1.0 X-Clacks-Overhead: GNU Terry Pratchett X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: multipart/mixed; boundary="DK+PbWDZZnC9udYX" Content-Disposition: inline X-Spam-Status: No, score=-13.8 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=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libstdc++@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++ mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 11 Oct 2021 19:37:29 -0000 --DK+PbWDZZnC9udYX Content-Type: text/plain; charset=us-ascii Content-Disposition: inline The end() function needs to consider whether the underlying vector is empty, not whether the match_results object is empty. That's because the underlying vector will always contain at least three elements for a match_results object that is "ready". It contains three extra elements which are stored in the vector but are not considered part of sequence, and so should not be part of the [begin(),end()) range. libstdc++-v3/ChangeLog: PR libstdc++/102667 * include/bits/regex.h (match_result::empty()): Optimize by calling the base function directly. (match_results::end()): Check _Base_type::empty() not empty(). * testsuite/28_regex/match_results/102667.C: New test. Tested powerpc64le-linux. Committed to trunk. --DK+PbWDZZnC9udYX Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="patch.txt" commit 84088dc4bb6a546c896a068dc201463493babf43 Author: Jonathan Wakely Date: Mon Oct 11 09:07:15 2021 libstdc++: Fix std::match_results::end() for failed matches [PR102667] The end() function needs to consider whether the underlying vector is empty, not whether the match_results object is empty. That's because the underlying vector will always contain at least three elements for a match_results object that is "ready". It contains three extra elements which are stored in the vector but are not considered part of sequence, and so should not be part of the [begin(),end()) range. libstdc++-v3/ChangeLog: PR libstdc++/102667 * include/bits/regex.h (match_result::empty()): Optimize by calling the base function directly. (match_results::end()): Check _Base_type::empty() not empty(). * testsuite/28_regex/match_results/102667.C: New test. diff --git a/libstdc++-v3/include/bits/regex.h b/libstdc++-v3/include/bits/regex.h index ff908da3e94..bf02bff7c49 100644 --- a/libstdc++-v3/include/bits/regex.h +++ b/libstdc++-v3/include/bits/regex.h @@ -1828,7 +1828,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 */ _GLIBCXX_NODISCARD bool empty() const noexcept - { return size() == 0; } + { return _Unchecked::size() <= 3; } ///@} @@ -1946,7 +1946,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 */ const_iterator end() const noexcept - { return _Base_type::end() - (empty() ? 0 : 3); } + { return _Base_type::end() - (_Base_type::empty() ? 0 : 3); } /** * @brief Gets an iterator to one-past-the-end of the collection. diff --git a/libstdc++-v3/testsuite/28_regex/match_results/102667.C b/libstdc++-v3/testsuite/28_regex/match_results/102667.C new file mode 100644 index 00000000000..9e38c9edaa4 --- /dev/null +++ b/libstdc++-v3/testsuite/28_regex/match_results/102667.C @@ -0,0 +1,39 @@ +// { dg-do run { target c++11 } } + +#include +#include + +void +test01() +{ + std::cmatch sm; + VERIFY( sm.empty() ); + VERIFY( sm.size() == 0 ); + VERIFY( sm.begin() == sm.end() ); // PR libstdc++/83600 + + bool matched = std::regex_match("a", sm, std::regex("b")); + VERIFY( ! matched ); + VERIFY( sm.ready() ); + VERIFY( sm.empty() ); + VERIFY( sm.size() == 0 ); + VERIFY( sm.begin() == sm.end() ); // PR libstdc++/102667 + + matched = std::regex_match("a", sm, std::regex("a")); + VERIFY( matched ); + VERIFY( sm.ready() ); + VERIFY( ! sm.empty() ); + VERIFY( sm.size() == 1 ); + VERIFY( (sm.end() - sm.begin()) == 1 ); + + matched = std::regex_search("abcd", sm, std::regex("(b)(c)")); + VERIFY( matched ); + VERIFY( sm.ready() ); + VERIFY( ! sm.empty() ); + VERIFY( sm.size() == 3 ); + VERIFY( (sm.end() - sm.begin()) == 3 ); +} + +int main() +{ + test01(); +} --DK+PbWDZZnC9udYX--