]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: std::basic_regex should treat '\0' as an ordinary char [PR84110]
authorJonathan Wakely <jwakely@redhat.com>
Wed, 29 Sep 2021 12:48:11 +0000 (13:48 +0100)
committerJonathan Wakely <jwakely@redhat.com>
Fri, 23 Jun 2023 12:19:24 +0000 (13:19 +0100)
When the input sequence contains a _CharT(0) character, the strchr call
in _Scanner<_CharT>::_M_scan_normal() will search for '\0' and so return
a pointer to the terminating null at the end of the string. This makes
the scanner think it's found a special character. Because it doesn't
match any of the actual special characters, we fall off the end of the
function (or assert in debug mode).

We should check for a null character explicitly and either treat it as
an ordinary character (for the ECMAScript grammar) or an error (for all
others). I'm not 100% sure that's right, but it seems consistent with
the POSIX RE rules where a '\0' means the end of the regex pattern or
the end of the sequence being matched.

Signed-off-by: Jonathan Wakely <jwakely@redhat.com>
libstdc++-v3/ChangeLog:

PR libstdc++/84110
* include/bits/regex_error.h (regex_constants::_S_null): New
error code for internal use.
* include/bits/regex_scanner.tcc (_Scanner::_M_scan_normal()):
Check for null character.
* testsuite/28_regex/basic_regex/84110.cc: New test.

(cherry picked from commit b701e1f8f6870c0f8cb4050674da489101dd05a5)

libstdc++-v3/include/bits/regex_error.h
libstdc++-v3/include/bits/regex_scanner.tcc
libstdc++-v3/testsuite/28_regex/basic_regex/84110.cc [new file with mode: 0644]

index b40351a39ccb3a39a9890ef6c65cf2dcf18f084b..f2899174352ecad5a0f15eade4d5b1e4f5f110dc 100644 (file)
@@ -61,6 +61,7 @@ namespace regex_constants
       _S_error_badrepeat,
       _S_error_complexity,
       _S_error_stack,
+      _S_null
     };
 
   /** The expression contained an invalid collating element name. */
index cb8a526ea1ab8c02978c8422ce024cd9a644aa66..a5a16af6cece0da1b8d514f1ab72aa74f5e03449 100644 (file)
@@ -176,6 +176,16 @@ namespace __detail
          _M_state = _S_state_in_brace;
          _M_token = _S_token_interval_begin;
        }
+      else if (__builtin_expect(__c == _CharT(0), false))
+       {
+         if (!_M_is_ecma())
+           {
+             __throw_regex_error(regex_constants::_S_null,
+                 "Unexpected null character in regular expression");
+           }
+         _M_token = _S_token_ord_char;
+         _M_value.assign(1, __c);
+       }
       else if (__c != ']' && __c != '}')
        {
          auto __it = _M_token_tbl;
diff --git a/libstdc++-v3/testsuite/28_regex/basic_regex/84110.cc b/libstdc++-v3/testsuite/28_regex/basic_regex/84110.cc
new file mode 100644 (file)
index 0000000..b9971dc
--- /dev/null
@@ -0,0 +1,39 @@
+// { dg-do run { target c++11 } }
+#include <regex>
+#include <string>
+#include <testsuite_hooks.h>
+
+void test01()
+{
+  const std::string s(1ul, '\0');
+  std::regex re(s);
+  VERIFY( std::regex_match(s, re) ); // PR libstdc++/84110
+
+#if __cpp_exceptions
+  using namespace std::regex_constants;
+  for (auto syn : {basic, extended, awk, grep, egrep})
+  {
+    try
+    {
+      std::regex{s, syn}; // '\0' is not valid for other grammars
+      VERIFY( false );
+    }
+    catch (const std::regex_error&)
+    {
+    }
+  }
+#endif
+}
+
+void test02()
+{
+  const std::string s("uh-\0h", 5);
+  std::regex re(s);
+  VERIFY( std::regex_match(s, re) );
+}
+
+int main()
+{
+  test01();
+  test02();
+}