c616bf
commit ee3db7c8f844556d35a66b3732bad9f44a086491
c616bf
Author: Jonathan Wakely <jwakely@redhat.com>
c616bf
Date:   Mon Sep 27 20:44:24 2021 +0100
c616bf
c616bf
    libstdc++: Fix handling of invalid ranges in std::regex [PR102447]
c616bf
    
c616bf
    std::regex currently allows invalid bracket ranges such as [\w-a] which
c616bf
    are only allowed by ECMAScript when in web browser compatibility mode.
c616bf
    It should be an error, because the start of the range is a character
c616bf
    class, not a single character. The current implementation of
c616bf
    _Compiler::_M_expression_term does not provide a way to reject this,
c616bf
    because we only remember a previous character, not whether we just
c616bf
    processed a character class (or collating symbol etc.)
c616bf
    
c616bf
    This patch replaces the pair<bool, CharT> used to emulate
c616bf
    optional<CharT> with a custom class closer to pair<tribool,CharT>. That
c616bf
    allows us to track three states, so that we can tell when we've just
c616bf
    seen a character class.
c616bf
    
c616bf
    With this additional state the code in _M_expression_term for processing
c616bf
    the _S_token_bracket_dash can be improved to correctly reject the [\w-a]
c616bf
    case, without regressing for valid cases such as [\w-] and [----].
c616bf
    
c616bf
    libstdc++-v3/ChangeLog:
c616bf
    
c616bf
            PR libstdc++/102447
c616bf
            * include/bits/regex_compiler.h (_Compiler::_BracketState): New
c616bf
            class.
c616bf
            (_Compiler::_BrackeyMatcher): New alias template.
c616bf
            (_Compiler::_M_expression_term): Change pair<bool, CharT>
c616bf
            parameter to _BracketState. Process first character for
c616bf
            ECMAScript syntax as well as POSIX.
c616bf
            * include/bits/regex_compiler.tcc
c616bf
            (_Compiler::_M_insert_bracket_matcher): Pass _BracketState.
c616bf
            (_Compiler::_M_expression_term): Use _BracketState to store
c616bf
            state between calls. Improve handling of dashes in ranges.
c616bf
            * testsuite/28_regex/algorithms/regex_match/cstring_bracket_01.cc:
c616bf
            Add more tests for ranges containing dashes. Check invalid
c616bf
            ranges with character class at the beginning.
c616bf
    
c616bf
    (cherry picked from commit 7ce3c230edf6e498e125c805a6dd313bf87dc439)
c616bf
c616bf
diff --git a/libstdc++-v3/include/bits/regex_compiler.h b/libstdc++-v3/include/bits/regex_compiler.h
c616bf
index 7e5c2073554..2eb1c3f7863 100644
c616bf
--- a/libstdc++-v3/include/bits/regex_compiler.h
c616bf
+++ b/libstdc++-v3/include/bits/regex_compiler.h
c616bf
@@ -122,13 +122,45 @@ namespace __detail
c616bf
 	void
c616bf
 	_M_insert_bracket_matcher(bool __neg);
c616bf
 
c616bf
-      // Returns true if successfully matched one term and should continue.
c616bf
+      // Cache of the last atom seen in a bracketed range expression.
c616bf
+      struct _BracketState
c616bf
+      {
c616bf
+	enum class _Type : char { _None, _Char, _Class } _M_type = _Type::_None;
c616bf
+	_CharT _M_char;
c616bf
+
c616bf
+	void
c616bf
+	set(_CharT __c) noexcept { _M_type = _Type::_Char; _M_char = __c; }
c616bf
+
c616bf
+	_GLIBCXX_NODISCARD _CharT
c616bf
+	get() const noexcept { return _M_char; }
c616bf
+
c616bf
+	void
c616bf
+	reset(_Type __t = _Type::_None) noexcept { _M_type = __t; }
c616bf
+
c616bf
+	explicit operator bool() const noexcept
c616bf
+	{ return _M_type != _Type::_None; }
c616bf
+
c616bf
+	// Previous token was a single character.
c616bf
+	_GLIBCXX_NODISCARD bool
c616bf
+	_M_is_char() const noexcept { return _M_type == _Type::_Char; }
c616bf
+
c616bf
+	// Previous token was a character class, equivalent class,
c616bf
+	// collating symbol etc.
c616bf
+	_GLIBCXX_NODISCARD bool
c616bf
+	_M_is_class() const noexcept { return _M_type == _Type::_Class; }
c616bf
+      };
c616bf
+
c616bf
+      template<bool __icase, bool __collate>
c616bf
+	using _BracketMatcher
c616bf
+	  = std::__detail::_BracketMatcher<_TraitsT, __icase, __collate>;
c616bf
+
c616bf
+      // Returns true if successfully parsed one term and should continue
c616bf
+      // compiling a bracket expression.
c616bf
       // Returns false if the compiler should move on.
c616bf
       template<bool __icase, bool __collate>
c616bf
 	bool
c616bf
-	_M_expression_term(pair<bool, _CharT>& __last_char,
c616bf
-			   _BracketMatcher<_TraitsT, __icase, __collate>&
c616bf
-			   __matcher);
c616bf
+	_M_expression_term(_BracketState& __last_char,
c616bf
+			   _BracketMatcher<__icase, __collate>& __matcher);
c616bf
 
c616bf
       int
c616bf
       _M_cur_int_value(int __radix);
c616bf
diff --git a/libstdc++-v3/include/bits/regex_compiler.tcc b/libstdc++-v3/include/bits/regex_compiler.tcc
c616bf
index b1169428afb..5877d30ba52 100644
c616bf
--- a/libstdc++-v3/include/bits/regex_compiler.tcc
c616bf
+++ b/libstdc++-v3/include/bits/regex_compiler.tcc
c616bf
@@ -140,7 +140,8 @@ namespace __detail
c616bf
 	return true;
c616bf
       if (this->_M_atom())
c616bf
 	{
c616bf
-	  while (this->_M_quantifier());
c616bf
+	  while (this->_M_quantifier())
c616bf
+	    ;
c616bf
 	  return true;
c616bf
 	}
c616bf
       return false;
c616bf
@@ -410,7 +411,7 @@ namespace __detail
c616bf
     _M_insert_character_class_matcher()
c616bf
     {
c616bf
       __glibcxx_assert(_M_value.size() == 1);
c616bf
-      _BracketMatcher<_TraitsT, __icase, __collate> __matcher
c616bf
+      _BracketMatcher<__icase, __collate> __matcher
c616bf
 	(_M_ctype.is(_CtypeT::upper, _M_value[0]), _M_traits);
c616bf
       __matcher._M_add_character_class(_M_value, false);
c616bf
       __matcher._M_ready();
c616bf
@@ -424,25 +425,17 @@ namespace __detail
c616bf
     _Compiler<_TraitsT>::
c616bf
     _M_insert_bracket_matcher(bool __neg)
c616bf
     {
c616bf
-      _BracketMatcher<_TraitsT, __icase, __collate> __matcher(__neg, _M_traits);
c616bf
-      pair<bool, _CharT> __last_char; // Optional<_CharT>
c616bf
-      __last_char.first = false;
c616bf
-      if (!(_M_flags & regex_constants::ECMAScript))
c616bf
-	{
c616bf
-	  if (_M_try_char())
c616bf
-	    {
c616bf
-	      __last_char.first = true;
c616bf
-	      __last_char.second = _M_value[0];
c616bf
-	    }
c616bf
-	  else if (_M_match_token(_ScannerT::_S_token_bracket_dash))
c616bf
-	    {
c616bf
-	      __last_char.first = true;
c616bf
-	      __last_char.second = '-';
c616bf
-	    }
c616bf
-	}
c616bf
-      while (_M_expression_term(__last_char, __matcher));
c616bf
-      if (__last_char.first)
c616bf
-	__matcher._M_add_char(__last_char.second);
c616bf
+      _BracketMatcher<__icase, __collate> __matcher(__neg, _M_traits);
c616bf
+      _BracketState __last_char;
c616bf
+      if (_M_try_char())
c616bf
+	__last_char.set(_M_value[0]);
c616bf
+      else if (_M_match_token(_ScannerT::_S_token_bracket_dash))
c616bf
+	// Dash as first character is a normal character.
c616bf
+	__last_char.set('-');
c616bf
+      while (_M_expression_term(__last_char, __matcher))
c616bf
+	;
c616bf
+      if (__last_char._M_is_char())
c616bf
+	__matcher._M_add_char(__last_char.get());
c616bf
       __matcher._M_ready();
c616bf
       _M_stack.push(_StateSeqT(
c616bf
 		      *_M_nfa,
c616bf
@@ -453,27 +446,27 @@ namespace __detail
c616bf
   template<bool __icase, bool __collate>
c616bf
     bool
c616bf
     _Compiler<_TraitsT>::
c616bf
-    _M_expression_term(pair<bool, _CharT>& __last_char,
c616bf
-		       _BracketMatcher<_TraitsT, __icase, __collate>& __matcher)
c616bf
+    _M_expression_term(_BracketState& __last_char,
c616bf
+		       _BracketMatcher<__icase, __collate>& __matcher)
c616bf
     {
c616bf
       if (_M_match_token(_ScannerT::_S_token_bracket_end))
c616bf
 	return false;
c616bf
 
c616bf
+      // Add any previously cached char into the matcher and update cache.
c616bf
       const auto __push_char = [&](_CharT __ch)
c616bf
       {
c616bf
-	if (__last_char.first)
c616bf
-	  __matcher._M_add_char(__last_char.second);
c616bf
-	else
c616bf
-	  __last_char.first = true;
c616bf
-	__last_char.second = __ch;
c616bf
+	if (__last_char._M_is_char())
c616bf
+	  __matcher._M_add_char(__last_char.get());
c616bf
+	__last_char.set(__ch);
c616bf
       };
c616bf
-      const auto __flush = [&]
c616bf
+      // Add any previously cached char into the matcher and update cache.
c616bf
+      const auto __push_class = [&]
c616bf
       {
c616bf
-	if (__last_char.first)
c616bf
-	  {
c616bf
-	    __matcher._M_add_char(__last_char.second);
c616bf
-	    __last_char.first = false;
c616bf
-	  }
c616bf
+        if (__last_char._M_is_char())
c616bf
+	  __matcher._M_add_char(__last_char.get());
c616bf
+	// We don't cache anything here, just record that the last thing
c616bf
+	// processed was a character class (or similar).
c616bf
+	__last_char.reset(_BracketState::_Type::_Class);
c616bf
       };
c616bf
 
c616bf
       if (_M_match_token(_ScannerT::_S_token_collsymbol))
c616bf
@@ -482,16 +475,16 @@ namespace __detail
c616bf
 	  if (__symbol.size() == 1)
c616bf
 	    __push_char(__symbol[0]);
c616bf
 	  else
c616bf
-	    __flush();
c616bf
+	    __push_class();
c616bf
 	}
c616bf
       else if (_M_match_token(_ScannerT::_S_token_equiv_class_name))
c616bf
 	{
c616bf
-	  __flush();
c616bf
+	  __push_class();
c616bf
 	  __matcher._M_add_equivalence_class(_M_value);
c616bf
 	}
c616bf
       else if (_M_match_token(_ScannerT::_S_token_char_class_name))
c616bf
 	{
c616bf
-	  __flush();
c616bf
+	  __push_class();
c616bf
 	  __matcher._M_add_character_class(_M_value, false);
c616bf
 	}
c616bf
       else if (_M_try_char())
c616bf
@@ -508,49 +501,50 @@ namespace __detail
c616bf
       // It turns out that no one reads BNFs ;)
c616bf
       else if (_M_match_token(_ScannerT::_S_token_bracket_dash))
c616bf
 	{
c616bf
-	  if (!__last_char.first)
c616bf
+	  if (_M_match_token(_ScannerT::_S_token_bracket_end))
c616bf
 	    {
c616bf
-	      if (!(_M_flags & regex_constants::ECMAScript))
c616bf
-		{
c616bf
-		  if (_M_match_token(_ScannerT::_S_token_bracket_end))
c616bf
-		    {
c616bf
-		      __push_char('-');
c616bf
-		      return false;
c616bf
-		    }
c616bf
-		  __throw_regex_error(
c616bf
-		    regex_constants::error_range,
c616bf
-		    "Unexpected dash in bracket expression. For POSIX syntax, "
c616bf
-		    "a dash is not treated literally only when it is at "
c616bf
-		    "beginning or end.");
c616bf
-		}
c616bf
+	      // For "-]" the dash is a literal character.
c616bf
 	      __push_char('-');
c616bf
+	      return false;
c616bf
 	    }
c616bf
-	  else
c616bf
+	  else if (__last_char._M_is_class())
c616bf
+	    {
c616bf
+	      // "\\w-" is invalid, start of range must be a single char.
c616bf
+	      __throw_regex_error(regex_constants::error_range,
c616bf
+		    "Invalid start of range in bracket expression.");
c616bf
+	    }
c616bf
+	  else if (__last_char._M_is_char())
c616bf
 	    {
c616bf
 	      if (_M_try_char())
c616bf
 		{
c616bf
-		  __matcher._M_make_range(__last_char.second, _M_value[0]);
c616bf
-		  __last_char.first = false;
c616bf
+		  // "x-y"
c616bf
+		  __matcher._M_make_range(__last_char.get(), _M_value[0]);
c616bf
+		  __last_char.reset();
c616bf
 		}
c616bf
 	      else if (_M_match_token(_ScannerT::_S_token_bracket_dash))
c616bf
 		{
c616bf
-		  __matcher._M_make_range(__last_char.second, '-');
c616bf
-		  __last_char.first = false;
c616bf
+		  // "x--"
c616bf
+		  __matcher._M_make_range(__last_char.get(), '-');
c616bf
+		  __last_char.reset();
c616bf
 		}
c616bf
 	      else
c616bf
-		{
c616bf
-		  if (_M_scanner._M_get_token()
c616bf
-		      != _ScannerT::_S_token_bracket_end)
c616bf
-		    __throw_regex_error(
c616bf
-		      regex_constants::error_range,
c616bf
-		      "Character is expected after a dash.");
c616bf
-		  __push_char('-');
c616bf
-		}
c616bf
+		__throw_regex_error(regex_constants::error_range,
c616bf
+		      "Invalid end of range in bracket expression.");
c616bf
 	    }
c616bf
+	  else if (_M_flags & regex_constants::ECMAScript)
c616bf
+	    {
c616bf
+	      // A dash that is not part of an existing range. Might be the
c616bf
+	      // start of a new range, or might just be a literal '-' char.
c616bf
+	      // Only ECMAScript allows that in the middle of a bracket expr.
c616bf
+	      __push_char('-');
c616bf
+	    }
c616bf
+	  else
c616bf
+	    __throw_regex_error(regex_constants::error_range,
c616bf
+				"Invalid dash in bracket expression.");
c616bf
 	}
c616bf
       else if (_M_match_token(_ScannerT::_S_token_quoted_class))
c616bf
 	{
c616bf
-	  __flush();
c616bf
+	  __push_class();
c616bf
 	  __matcher._M_add_character_class(_M_value,
c616bf
 					   _M_ctype.is(_CtypeT::upper,
c616bf
 						       _M_value[0]));
c616bf
diff --git a/libstdc++-v3/testsuite/28_regex/algorithms/regex_match/cstring_bracket_01.cc b/libstdc++-v3/testsuite/28_regex/algorithms/regex_match/cstring_bracket_01.cc
c616bf
index 236ab663fc0..57088f5af83 100644
c616bf
--- a/libstdc++-v3/testsuite/28_regex/algorithms/regex_match/cstring_bracket_01.cc
c616bf
+++ b/libstdc++-v3/testsuite/28_regex/algorithms/regex_match/cstring_bracket_01.cc
c616bf
@@ -68,6 +68,16 @@ test01()
c616bf
 void
c616bf
 test02()
c616bf
 {
c616bf
+  VERIFY(regex_match("-", regex("[-]", regex_constants::ECMAScript)));
c616bf
+  VERIFY(regex_match("-", regex("[--]", regex_constants::ECMAScript)));
c616bf
+  VERIFY(regex_match("-", regex("[---]", regex_constants::ECMAScript)));
c616bf
+  VERIFY(regex_match("-", regex("[----]", regex_constants::ECMAScript)));
c616bf
+  VERIFY(regex_match("-", regex("[-----]", regex_constants::ECMAScript)));
c616bf
+
c616bf
+  VERIFY(regex_match("-", regex("[-]", regex_constants::extended)));
c616bf
+  VERIFY(regex_match("-", regex("[--]", regex_constants::extended)));
c616bf
+  VERIFY(regex_match("-", regex("[---]", regex_constants::extended)));
c616bf
+  VERIFY(regex_match("-", regex("[----]", regex_constants::extended)));
c616bf
   try
c616bf
   {
c616bf
     std::regex re("[-----]", std::regex::extended);
c616bf
@@ -77,7 +87,6 @@ test02()
c616bf
   {
c616bf
     VERIFY(e.code() == std::regex_constants::error_range);
c616bf
   }
c616bf
-  std::regex re("[-----]", std::regex::ECMAScript);
c616bf
 
c616bf
   VERIFY(!regex_match("b", regex("[-ac]", regex_constants::extended)));
c616bf
   VERIFY(!regex_match("b", regex("[ac-]", regex_constants::extended)));
c616bf
@@ -92,7 +101,27 @@ test02()
c616bf
   }
c616bf
   catch (const std::regex_error& e)
c616bf
   {
c616bf
+    VERIFY(e.code() == std::regex_constants::error_range);
c616bf
+  }
c616bf
+  try
c616bf
+  {
c616bf
+    regex("[@--]", regex_constants::extended);
c616bf
+    VERIFY(false);
c616bf
   }
c616bf
+  catch (const std::regex_error& e)
c616bf
+  {
c616bf
+    VERIFY(e.code() == std::regex_constants::error_range);
c616bf
+  }
c616bf
+  try
c616bf
+  {
c616bf
+    regex("[--%]", regex_constants::extended);
c616bf
+    VERIFY(false);
c616bf
+  }
c616bf
+  catch (const std::regex_error& e)
c616bf
+  {
c616bf
+    VERIFY(e.code() == std::regex_constants::error_range);
c616bf
+  }
c616bf
+
c616bf
   VERIFY(regex_match("].", regex("[][.hyphen.]-0]*", regex_constants::extended)));
c616bf
 }
c616bf
 
c616bf
@@ -157,6 +186,36 @@ test06()
c616bf
   VERIFY(regex_match("a-", debian_cron_namespace_ok));
c616bf
 }
c616bf
 
c616bf
+// libstdc++/102447
c616bf
+void
c616bf
+test07()
c616bf
+{
c616bf
+  VERIFY(regex_match("-", std::regex("[\\w-]", std::regex::ECMAScript)));
c616bf
+  VERIFY(regex_match("a", std::regex("[\\w-]", std::regex::ECMAScript)));
c616bf
+  VERIFY(regex_match("-", std::regex("[a-]", std::regex::ECMAScript)));
c616bf
+  VERIFY(regex_match("a", std::regex("[a-]", std::regex::ECMAScript)));
c616bf
+
c616bf
+  try
c616bf
+  {
c616bf
+    std::regex re("[\\w-a]", std::regex::ECMAScript);
c616bf
+    VERIFY(false);
c616bf
+  }
c616bf
+  catch (const std::regex_error& e)
c616bf
+  {
c616bf
+    VERIFY(e.code() == std::regex_constants::error_range);
c616bf
+  }
c616bf
+
c616bf
+  try
c616bf
+  {
c616bf
+    std::regex re("[\\w--]", std::regex::ECMAScript);
c616bf
+    VERIFY(false);
c616bf
+  }
c616bf
+  catch (const std::regex_error& e)
c616bf
+  {
c616bf
+    VERIFY(e.code() == std::regex_constants::error_range);
c616bf
+  }
c616bf
+}
c616bf
+
c616bf
 int
c616bf
 main()
c616bf
 {
c616bf
@@ -166,6 +225,7 @@ main()
c616bf
   test04();
c616bf
   test05();
c616bf
   test06();
c616bf
+  test07();
c616bf
 
c616bf
   return 0;
c616bf
 }
c616bf
c616bf
commit 1851cc4c5f2666dfdec53a2ada57095ffc59e08b
c616bf
Author: Jonathan Wakely <jwakely@redhat.com>
c616bf
Date:   Mon Dec 13 13:36:33 2021 +0000
c616bf
c616bf
    libstdc++: Fix non-reserved name in <regex> header
c616bf
    
c616bf
    libstdc++-v3/ChangeLog:
c616bf
    
c616bf
            * include/bits/regex_compiler.tcc (_Compiler::_M_match_token):
c616bf
            Use reserved name for parameter.
c616bf
            * testsuite/17_intro/names.cc: Check "token".
c616bf
    
c616bf
    (cherry picked from commit b0e6a257f1862e217cdf19332ea0f7bad56dcddc)
c616bf
c616bf
diff --git a/libstdc++-v3/include/bits/regex_compiler.tcc b/libstdc++-v3/include/bits/regex_compiler.tcc
c616bf
index 8af920e5fe9..b1169428afb 100644
c616bf
--- a/libstdc++-v3/include/bits/regex_compiler.tcc
c616bf
+++ b/libstdc++-v3/include/bits/regex_compiler.tcc
c616bf
@@ -586,9 +586,9 @@ namespace __detail
c616bf
   template<typename _TraitsT>
c616bf
     bool
c616bf
     _Compiler<_TraitsT>::
c616bf
-    _M_match_token(_TokenT token)
c616bf
+    _M_match_token(_TokenT __token)
c616bf
     {
c616bf
-      if (token == _M_scanner._M_get_token())
c616bf
+      if (__token == _M_scanner._M_get_token())
c616bf
 	{
c616bf
 	  _M_value = _M_scanner._M_get_value();
c616bf
 	  _M_scanner._M_advance();
c616bf
diff --git a/libstdc++-v3/testsuite/17_intro/names.cc b/libstdc++-v3/testsuite/17_intro/names.cc
c616bf
index d758138dfb1..6c06aba7228 100644
c616bf
--- a/libstdc++-v3/testsuite/17_intro/names.cc
c616bf
+++ b/libstdc++-v3/testsuite/17_intro/names.cc
c616bf
@@ -99,6 +99,7 @@
c616bf
 #define z (
c616bf
 
c616bf
 #define tmp (
c616bf
+#define token (
c616bf
 
c616bf
 #if __cplusplus < 201103L
c616bf
 #define uses_allocator  (
c616bf
--- a/libstdc++-v3/include/bits/c++config.orig	2022-07-08 15:06:14.083231445 -0400
c616bf
+++ b/libstdc++-v3/include/bits/c++config	2022-07-08 15:06:41.733247859 -0400
c616bf
@@ -99,6 +99,12 @@
c616bf
 # define _GLIBCXX_ABI_TAG_CXX11 __attribute ((__abi_tag__ ("cxx11")))
c616bf
 #endif
c616bf
 
c616bf
+// Macro to warn about unused results.
c616bf
+#if __cplusplus >= 201703L
c616bf
+# define _GLIBCXX_NODISCARD [[__nodiscard__]]
c616bf
+#else
c616bf
+# define _GLIBCXX_NODISCARD
c616bf
+#endif
c616bf
 
c616bf
 #if __cplusplus
c616bf