Blame SOURCES/gcc11-rh2106262.patch

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