public inbox for libstdc++-cvs@sourceware.org
help / color / mirror / Atom feed
From: Jonathan Wakely <redi@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org
Subject: [gcc r11-9099] libstdc++: Fix std::match_results::end() for failed matches [PR102667]
Date: Tue, 12 Oct 2021 10:58:21 +0000 (GMT)	[thread overview]
Message-ID: <20211012105821.062443858408@sourceware.org> (raw)

https://gcc.gnu.org/g:2560bab6ceb7c1eb7c5cdadb5f0a608ac166b829

commit r11-9099-g2560bab6ceb7c1eb7c5cdadb5f0a608ac166b829
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Mon Oct 11 09:07:15 2021 +0100

    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.
    
    (cherry picked from commit 84088dc4bb6a546c896a068dc201463493babf43)

Diff:
---
 libstdc++-v3/include/bits/regex.h                  |  4 +--
 .../testsuite/28_regex/match_results/102667.C      | 39 ++++++++++++++++++++++
 2 files changed, 41 insertions(+), 2 deletions(-)

diff --git a/libstdc++-v3/include/bits/regex.h b/libstdc++-v3/include/bits/regex.h
index ac10fa184c6..a2eede6f3e4 100644
--- a/libstdc++-v3/include/bits/regex.h
+++ b/libstdc++-v3/include/bits/regex.h
@@ -1804,7 +1804,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
        */
       _GLIBCXX_NODISCARD bool
       empty() const noexcept
-      { return size() == 0; }
+      { return _Base_type::size() <= 3; }
 
       ///@}
 
@@ -1922,7 +1922,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 <regex>
+#include <testsuite_hooks.h>
+
+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();
+}


                 reply	other threads:[~2021-10-12 10:58 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20211012105821.062443858408@sourceware.org \
    --to=redi@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    --cc=libstdc++-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).