Blame SOURCES/gcc11-rh2106262.patch

694bc6
commit c725028a8bb9478ec84332641147ad12b9236922
694bc6
Author: Jonathan Wakely <jwakely@redhat.com>
694bc6
Date:   Tue Dec 14 14:32:35 2021 +0000
694bc6
694bc6
    libstdc++: Fix handling of invalid ranges in std::regex [PR102447]
694bc6
    
694bc6
    std::regex currently allows invalid bracket ranges such as [\w-a] which
694bc6
    are only allowed by ECMAScript when in web browser compatibility mode.
694bc6
    It should be an error, because the start of the range is a character
694bc6
    class, not a single character. The current implementation of
694bc6
    _Compiler::_M_expression_term does not provide a way to reject this,
694bc6
    because we only remember a previous character, not whether we just
694bc6
    processed a character class (or collating symbol etc.)
694bc6
    
694bc6
    This patch replaces the pair<bool, CharT> used to emulate
694bc6
    optional<CharT> with a custom class closer to pair<tribool,CharT>. That
694bc6
    allows us to track three states, so that we can tell when we've just
694bc6
    seen a character class.
694bc6
    
694bc6
    With this additional state the code in _M_expression_term for processing
694bc6
    the _S_token_bracket_dash can be improved to correctly reject the [\w-a]
694bc6
    case, without regressing for valid cases such as [\w-] and [----].
694bc6
    
694bc6
    libstdc++-v3/ChangeLog:
694bc6
    
694bc6
            PR libstdc++/102447
694bc6
            * include/bits/regex_compiler.h (_Compiler::_BracketState): New
694bc6
            class.
694bc6
            (_Compiler::_BrackeyMatcher): New alias template.
694bc6
            (_Compiler::_M_expression_term): Change pair<bool, CharT>
694bc6
            parameter to _BracketState. Process first character for
694bc6
            ECMAScript syntax as well as POSIX.
694bc6
            * include/bits/regex_compiler.tcc
694bc6
            (_Compiler::_M_insert_bracket_matcher): Pass _BracketState.
694bc6
            (_Compiler::_M_expression_term): Use _BracketState to store
694bc6
            state between calls. Improve handling of dashes in ranges.
694bc6
            * testsuite/28_regex/algorithms/regex_match/cstring_bracket_01.cc:
694bc6
            Add more tests for ranges containing dashes. Check invalid
694bc6
            ranges with character class at the beginning.
694bc6
    
694bc6
    (cherry picked from commit 7ce3c230edf6e498e125c805a6dd313bf87dc439)
694bc6
694bc6
diff --git a/libstdc++-v3/include/bits/regex_compiler.h b/libstdc++-v3/include/bits/regex_compiler.h
694bc6
index f224fcb06e0..aa19df2bf9a 100644
694bc6
--- a/libstdc++-v3/include/bits/regex_compiler.h
694bc6
+++ b/libstdc++-v3/include/bits/regex_compiler.h
694bc6
@@ -122,13 +122,45 @@ namespace __detail
694bc6
 	void
694bc6
 	_M_insert_bracket_matcher(bool __neg);
694bc6
 
694bc6
-      // Returns true if successfully matched one term and should continue.
694bc6
+      // Cache of the last atom seen in a bracketed range expression.
694bc6
+      struct _BracketState
694bc6
+      {
694bc6
+	enum class _Type : char { _None, _Char, _Class } _M_type = _Type::_None;
694bc6
+	_CharT _M_char;
694bc6
+
694bc6
+	void
694bc6
+	set(_CharT __c) noexcept { _M_type = _Type::_Char; _M_char = __c; }
694bc6
+
694bc6
+	_GLIBCXX_NODISCARD _CharT
694bc6
+	get() const noexcept { return _M_char; }
694bc6
+
694bc6
+	void
694bc6
+	reset(_Type __t = _Type::_None) noexcept { _M_type = __t; }
694bc6
+
694bc6
+	explicit operator bool() const noexcept
694bc6
+	{ return _M_type != _Type::_None; }
694bc6
+
694bc6
+	// Previous token was a single character.
694bc6
+	_GLIBCXX_NODISCARD bool
694bc6
+	_M_is_char() const noexcept { return _M_type == _Type::_Char; }
694bc6
+
694bc6
+	// Previous token was a character class, equivalent class,
694bc6
+	// collating symbol etc.
694bc6
+	_GLIBCXX_NODISCARD bool
694bc6
+	_M_is_class() const noexcept { return _M_type == _Type::_Class; }
694bc6
+      };
694bc6
+
694bc6
+      template<bool __icase, bool __collate>
694bc6
+	using _BracketMatcher
694bc6
+	  = std::__detail::_BracketMatcher<_TraitsT, __icase, __collate>;
694bc6
+
694bc6
+      // Returns true if successfully parsed one term and should continue
694bc6
+      // compiling a bracket expression.
694bc6
       // Returns false if the compiler should move on.
694bc6
       template<bool __icase, bool __collate>
694bc6
 	bool
694bc6
-	_M_expression_term(pair<bool, _CharT>& __last_char,
694bc6
-			   _BracketMatcher<_TraitsT, __icase, __collate>&
694bc6
-			   __matcher);
694bc6
+	_M_expression_term(_BracketState& __last_char,
694bc6
+			   _BracketMatcher<__icase, __collate>& __matcher);
694bc6
 
694bc6
       int
694bc6
       _M_cur_int_value(int __radix);
694bc6
diff --git a/libstdc++-v3/include/bits/regex_compiler.tcc b/libstdc++-v3/include/bits/regex_compiler.tcc
694bc6
index ea07bc2428e..7769a9e63a3 100644
694bc6
--- a/libstdc++-v3/include/bits/regex_compiler.tcc
694bc6
+++ b/libstdc++-v3/include/bits/regex_compiler.tcc
694bc6
@@ -403,7 +403,7 @@ namespace __detail
694bc6
     _M_insert_character_class_matcher()
694bc6
     {
694bc6
       __glibcxx_assert(_M_value.size() == 1);
694bc6
-      _BracketMatcher<_TraitsT, __icase, __collate> __matcher
694bc6
+      _BracketMatcher<__icase, __collate> __matcher
694bc6
 	(_M_ctype.is(_CtypeT::upper, _M_value[0]), _M_traits);
694bc6
       __matcher._M_add_character_class(_M_value, false);
694bc6
       __matcher._M_ready();
694bc6
@@ -424,25 +424,17 @@ namespace __detail
694bc6
     _Compiler<_TraitsT>::
694bc6
     _M_insert_bracket_matcher(bool __neg)
694bc6
     {
694bc6
-      _BracketMatcher<_TraitsT, __icase, __collate> __matcher(__neg, _M_traits);
694bc6
-      pair<bool, _CharT> __last_char; // Optional<_CharT>
694bc6
-      __last_char.first = false;
694bc6
-      if (!(_M_flags & regex_constants::ECMAScript))
694bc6
-	{
694bc6
-	  if (_M_try_char())
694bc6
-	    {
694bc6
-	      __last_char.first = true;
694bc6
-	      __last_char.second = _M_value[0];
694bc6
-	    }
694bc6
-	  else if (_M_match_token(_ScannerT::_S_token_bracket_dash))
694bc6
-	    {
694bc6
-	      __last_char.first = true;
694bc6
-	      __last_char.second = '-';
694bc6
-	    }
694bc6
-	}
694bc6
-      while (_M_expression_term(__last_char, __matcher));
694bc6
-      if (__last_char.first)
694bc6
-	__matcher._M_add_char(__last_char.second);
694bc6
+      _BracketMatcher<__icase, __collate> __matcher(__neg, _M_traits);
694bc6
+      _BracketState __last_char;
694bc6
+      if (_M_try_char())
694bc6
+	__last_char.set(_M_value[0]);
694bc6
+      else if (_M_match_token(_ScannerT::_S_token_bracket_dash))
694bc6
+	// Dash as first character is a normal character.
694bc6
+	__last_char.set('-');
694bc6
+      while (_M_expression_term(__last_char, __matcher))
694bc6
+	;
694bc6
+      if (__last_char._M_is_char())
694bc6
+	__matcher._M_add_char(__last_char.get());
694bc6
       __matcher._M_ready();
694bc6
       _M_stack.push(_StateSeqT(
694bc6
 		      *_M_nfa,
694bc6
@@ -447,27 +438,27 @@ namespace __detail
694bc6
   template<bool __icase, bool __collate>
694bc6
     bool
694bc6
     _Compiler<_TraitsT>::
694bc6
-    _M_expression_term(pair<bool, _CharT>& __last_char,
694bc6
-		       _BracketMatcher<_TraitsT, __icase, __collate>& __matcher)
694bc6
+    _M_expression_term(_BracketState& __last_char,
694bc6
+		       _BracketMatcher<__icase, __collate>& __matcher)
694bc6
     {
694bc6
       if (_M_match_token(_ScannerT::_S_token_bracket_end))
694bc6
 	return false;
694bc6
 
694bc6
+      // Add any previously cached char into the matcher and update cache.
694bc6
       const auto __push_char = [&](_CharT __ch)
694bc6
       {
694bc6
-	if (__last_char.first)
694bc6
-	  __matcher._M_add_char(__last_char.second);
694bc6
-	else
694bc6
-	  __last_char.first = true;
694bc6
-	__last_char.second = __ch;
694bc6
+	if (__last_char._M_is_char())
694bc6
+	  __matcher._M_add_char(__last_char.get());
694bc6
+	__last_char.set(__ch);
694bc6
       };
694bc6
-      const auto __flush = [&]
694bc6
+      // Add any previously cached char into the matcher and update cache.
694bc6
+      const auto __push_class = [&]
694bc6
       {
694bc6
-	if (__last_char.first)
694bc6
-	  {
694bc6
-	    __matcher._M_add_char(__last_char.second);
694bc6
-	    __last_char.first = false;
694bc6
-	  }
694bc6
+        if (__last_char._M_is_char())
694bc6
+	  __matcher._M_add_char(__last_char.get());
694bc6
+	// We don't cache anything here, just record that the last thing
694bc6
+	// processed was a character class (or similar).
694bc6
+	__last_char.reset(_BracketState::_Type::_Class);
694bc6
       };
694bc6
 
694bc6
       if (_M_match_token(_ScannerT::_S_token_collsymbol))
694bc6
@@ -476,16 +467,16 @@ namespace __detail
694bc6
 	  if (__symbol.size() == 1)
694bc6
 	    __push_char(__symbol[0]);
694bc6
 	  else
694bc6
-	    __flush();
694bc6
+	    __push_class();
694bc6
 	}
694bc6
       else if (_M_match_token(_ScannerT::_S_token_equiv_class_name))
694bc6
 	{
694bc6
-	  __flush();
694bc6
+	  __push_class();
694bc6
 	  __matcher._M_add_equivalence_class(_M_value);
694bc6
 	}
694bc6
       else if (_M_match_token(_ScannerT::_S_token_char_class_name))
694bc6
 	{
694bc6
-	  __flush();
694bc6
+	  __push_class();
694bc6
 	  __matcher._M_add_character_class(_M_value, false);
694bc6
 	}
694bc6
       else if (_M_try_char())
694bc6
@@ -502,49 +493,50 @@ namespace __detail
694bc6
       // It turns out that no one reads BNFs ;)
694bc6
       else if (_M_match_token(_ScannerT::_S_token_bracket_dash))
694bc6
 	{
694bc6
-	  if (!__last_char.first)
694bc6
+	  if (_M_match_token(_ScannerT::_S_token_bracket_end))
694bc6
 	    {
694bc6
-	      if (!(_M_flags & regex_constants::ECMAScript))
694bc6
-		{
694bc6
-		  if (_M_match_token(_ScannerT::_S_token_bracket_end))
694bc6
-		    {
694bc6
-		      __push_char('-');
694bc6
-		      return false;
694bc6
-		    }
694bc6
-		  __throw_regex_error(
694bc6
-		    regex_constants::error_range,
694bc6
-		    "Unexpected dash in bracket expression. For POSIX syntax, "
694bc6
-		    "a dash is not treated literally only when it is at "
694bc6
-		    "beginning or end.");
694bc6
-		}
694bc6
+	      // For "-]" the dash is a literal character.
694bc6
 	      __push_char('-');
694bc6
+	      return false;
694bc6
 	    }
694bc6
-	  else
694bc6
+	  else if (__last_char._M_is_class())
694bc6
+	    {
694bc6
+	      // "\\w-" is invalid, start of range must be a single char.
694bc6
+	      __throw_regex_error(regex_constants::error_range,
694bc6
+		    "Invalid start of range in bracket expression.");
694bc6
+	    }
694bc6
+	  else if (__last_char._M_is_char())
694bc6
 	    {
694bc6
 	      if (_M_try_char())
694bc6
 		{
694bc6
-		  __matcher._M_make_range(__last_char.second, _M_value[0]);
694bc6
-		  __last_char.first = false;
694bc6
+		  // "x-y"
694bc6
+		  __matcher._M_make_range(__last_char.get(), _M_value[0]);
694bc6
+		  __last_char.reset();
694bc6
 		}
694bc6
 	      else if (_M_match_token(_ScannerT::_S_token_bracket_dash))
694bc6
 		{
694bc6
-		  __matcher._M_make_range(__last_char.second, '-');
694bc6
-		  __last_char.first = false;
694bc6
+		  // "x--"
694bc6
+		  __matcher._M_make_range(__last_char.get(), '-');
694bc6
+		  __last_char.reset();
694bc6
 		}
694bc6
 	      else
694bc6
-		{
694bc6
-		  if (_M_scanner._M_get_token()
694bc6
-		      != _ScannerT::_S_token_bracket_end)
694bc6
-		    __throw_regex_error(
694bc6
-		      regex_constants::error_range,
694bc6
-		      "Character is expected after a dash.");
694bc6
-		  __push_char('-');
694bc6
-		}
694bc6
+		__throw_regex_error(regex_constants::error_range,
694bc6
+		      "Invalid end of range in bracket expression.");
694bc6
 	    }
694bc6
+	  else if (_M_flags & regex_constants::ECMAScript)
694bc6
+	    {
694bc6
+	      // A dash that is not part of an existing range. Might be the
694bc6
+	      // start of a new range, or might just be a literal '-' char.
694bc6
+	      // Only ECMAScript allows that in the middle of a bracket expr.
694bc6
+	      __push_char('-');
694bc6
+	    }
694bc6
+	  else
694bc6
+	    __throw_regex_error(regex_constants::error_range,
694bc6
+				"Invalid dash in bracket expression.");
694bc6
 	}
694bc6
       else if (_M_match_token(_ScannerT::_S_token_quoted_class))
694bc6
 	{
694bc6
-	  __flush();
694bc6
+	  __push_class();
694bc6
 	  __matcher._M_add_character_class(_M_value,
694bc6
 					   _M_ctype.is(_CtypeT::upper,
694bc6
 						       _M_value[0]));
694bc6
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
694bc6
index 7df70604ea6..0d76e63da7b 100644
694bc6
--- a/libstdc++-v3/testsuite/28_regex/algorithms/regex_match/cstring_bracket_01.cc
694bc6
+++ b/libstdc++-v3/testsuite/28_regex/algorithms/regex_match/cstring_bracket_01.cc
694bc6
@@ -69,6 +69,16 @@ test01()
694bc6
 void
694bc6
 test02()
694bc6
 {
694bc6
+  VERIFY(regex_match("-", regex("[-]", regex_constants::ECMAScript)));
694bc6
+  VERIFY(regex_match("-", regex("[--]", regex_constants::ECMAScript)));
694bc6
+  VERIFY(regex_match("-", regex("[---]", regex_constants::ECMAScript)));
694bc6
+  VERIFY(regex_match("-", regex("[----]", regex_constants::ECMAScript)));
694bc6
+  VERIFY(regex_match("-", regex("[-----]", regex_constants::ECMAScript)));
694bc6
+
694bc6
+  VERIFY(regex_match("-", regex("[-]", regex_constants::extended)));
694bc6
+  VERIFY(regex_match("-", regex("[--]", regex_constants::extended)));
694bc6
+  VERIFY(regex_match("-", regex("[---]", regex_constants::extended)));
694bc6
+  VERIFY(regex_match("-", regex("[----]", regex_constants::extended)));
694bc6
   try
694bc6
   {
694bc6
     std::regex re("[-----]", std::regex::extended);
694bc6
@@ -78,7 +88,6 @@ test02()
694bc6
   {
694bc6
     VERIFY(e.code() == std::regex_constants::error_range);
694bc6
   }
694bc6
-  std::regex re("[-----]", std::regex::ECMAScript);
694bc6
 
694bc6
   VERIFY(!regex_match("b", regex("[-ac]", regex_constants::extended)));
694bc6
   VERIFY(!regex_match("b", regex("[ac-]", regex_constants::extended)));
694bc6
@@ -93,7 +102,27 @@ test02()
694bc6
   }
694bc6
   catch (const std::regex_error& e)
694bc6
   {
694bc6
+    VERIFY(e.code() == std::regex_constants::error_range);
694bc6
+  }
694bc6
+  try
694bc6
+  {
694bc6
+    regex("[@--]", regex_constants::extended);
694bc6
+    VERIFY(false);
694bc6
   }
694bc6
+  catch (const std::regex_error& e)
694bc6
+  {
694bc6
+    VERIFY(e.code() == std::regex_constants::error_range);
694bc6
+  }
694bc6
+  try
694bc6
+  {
694bc6
+    regex("[--%]", regex_constants::extended);
694bc6
+    VERIFY(false);
694bc6
+  }
694bc6
+  catch (const std::regex_error& e)
694bc6
+  {
694bc6
+    VERIFY(e.code() == std::regex_constants::error_range);
694bc6
+  }
694bc6
+
694bc6
   VERIFY(regex_match("].", regex("[][.hyphen.]-0]*", regex_constants::extended)));
694bc6
 }
694bc6
 
694bc6
@@ -158,6 +187,36 @@ test06()
694bc6
   VERIFY(regex_match("a-", debian_cron_namespace_ok));
694bc6
 }
694bc6
 
694bc6
+// libstdc++/102447
694bc6
+void
694bc6
+test07()
694bc6
+{
694bc6
+  VERIFY(regex_match("-", std::regex("[\\w-]", std::regex::ECMAScript)));
694bc6
+  VERIFY(regex_match("a", std::regex("[\\w-]", std::regex::ECMAScript)));
694bc6
+  VERIFY(regex_match("-", std::regex("[a-]", std::regex::ECMAScript)));
694bc6
+  VERIFY(regex_match("a", std::regex("[a-]", std::regex::ECMAScript)));
694bc6
+
694bc6
+  try
694bc6
+  {
694bc6
+    std::regex re("[\\w-a]", std::regex::ECMAScript);
694bc6
+    VERIFY(false);
694bc6
+  }
694bc6
+  catch (const std::regex_error& e)
694bc6
+  {
694bc6
+    VERIFY(e.code() == std::regex_constants::error_range);
694bc6
+  }
694bc6
+
694bc6
+  try
694bc6
+  {
694bc6
+    std::regex re("[\\w--]", std::regex::ECMAScript);
694bc6
+    VERIFY(false);
694bc6
+  }
694bc6
+  catch (const std::regex_error& e)
694bc6
+  {
694bc6
+    VERIFY(e.code() == std::regex_constants::error_range);
694bc6
+  }
694bc6
+}
694bc6
+
694bc6
 int
694bc6
 main()
694bc6
 {
694bc6
@@ -167,6 +226,7 @@ main()
694bc6
   test04();
694bc6
   test05();
694bc6
   test06();
694bc6
+  test07();
694bc6
 
694bc6
   return 0;
694bc6
 }