diff --git a/libstdc++-v3/include/bits/regex_error.h b/libstdc++-v3/include/bits/regex_error.h index d3713fa5f47..722ce26cda3 100644 --- a/libstdc++-v3/include/bits/regex_error.h +++ b/libstdc++-v3/include/bits/regex_error.h @@ -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. */ diff --git a/libstdc++-v3/include/bits/regex_scanner.tcc b/libstdc++-v3/include/bits/regex_scanner.tcc index b2b709ce3cb..d81627dc3e9 100644 --- a/libstdc++-v3/include/bits/regex_scanner.tcc +++ b/libstdc++-v3/include/bits/regex_scanner.tcc @@ -175,6 +175,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 index 00000000000..b9971dcaac5 --- /dev/null +++ b/libstdc++-v3/testsuite/28_regex/basic_regex/84110.cc @@ -0,0 +1,39 @@ +// { dg-do run { target c++11 } } +#include +#include +#include + +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(); +}