Blame SOURCES/gcc8-rh2001788.patch

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