From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id A03A03858002; Fri, 23 Jun 2023 16:12:42 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A03A03858002 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1687536762; bh=vCykTocXZF8lRqt96Gc9tOeSn0/77fAJOgdxYDr+NjA=; h=From:To:Subject:Date:From; b=L5KpqvBEZvu1YYrk20jv5MODZtB6tBjGLrDjwjqkaRhQB77DGnTdMwm8bFjR1Eh+o ByYRbPm8VAP5uA+ihnF+81OpC6vSgr5vHAlSZcAMp+htNpqYDgnqXdunOEkvVgEBgl LgrOldk0T29/UUbZ3Q+K0uxh2QIp5dbQoaVJnZ2U= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jonathan Wakely To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r10-11464] libstdc++: Check for invalid syntax_option_type values in X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/releases/gcc-10 X-Git-Oldrev: 884751cc4f24ae92f550a4733632e9c54e7182d8 X-Git-Newrev: 7e52977d21a916de480e310aca9e64c6a30d912f Message-Id: <20230623161242.A03A03858002@sourceware.org> Date: Fri, 23 Jun 2023 16:12:42 +0000 (GMT) List-Id: https://gcc.gnu.org/g:7e52977d21a916de480e310aca9e64c6a30d912f commit r10-11464-g7e52977d21a916de480e310aca9e64c6a30d912f Author: Jonathan Wakely Date: Wed Sep 29 13:48:15 2021 +0100 libstdc++: Check for invalid syntax_option_type values in The standard says that it is invalid for more than one grammar element to be set in a value of type regex_constants::syntax_option_type. This adds a check in the regex compiler andthrows an exception if an invalid value is used. Signed-off-by: Jonathan Wakely libstdc++-v3/ChangeLog: * include/bits/regex_compiler.h (_Compiler::_S_validate): New function. * include/bits/regex_compiler.tcc (_Compiler::_Compiler): Use _S_validate to check flags. * include/bits/regex_error.h (_S_grammar): New error code for internal use. * testsuite/28_regex/basic_regex/ctors/grammar.cc: New test. (cherry picked from commit 9ca4c42a3b756e54a92ff8e1ac6c396b680b7839) Diff: --- libstdc++-v3/include/bits/regex_compiler.h | 20 ++++++++ libstdc++-v3/include/bits/regex_compiler.tcc | 10 +--- libstdc++-v3/include/bits/regex_error.h | 3 +- .../28_regex/basic_regex/ctors/grammar.cc | 53 ++++++++++++++++++++++ 4 files changed, 76 insertions(+), 10 deletions(-) diff --git a/libstdc++-v3/include/bits/regex_compiler.h b/libstdc++-v3/include/bits/regex_compiler.h index 809ae419f5e..df7e41f1a0d 100644 --- a/libstdc++-v3/include/bits/regex_compiler.h +++ b/libstdc++-v3/include/bits/regex_compiler.h @@ -144,6 +144,26 @@ namespace __detail return ret; } + static _FlagT + _S_validate(_FlagT __f) + { + using namespace regex_constants; + switch (__f & (ECMAScript|basic|extended|awk|grep|egrep)) + { + case ECMAScript: + case basic: + case extended: + case awk: + case grep: + case egrep: + return __f; + case _FlagT(0): + return __f | ECMAScript; + default: + std::__throw_regex_error(_S_grammar, "conflicting grammar options"); + } + } + _FlagT _M_flags; _ScannerT _M_scanner; shared_ptr<_RegexT> _M_nfa; diff --git a/libstdc++-v3/include/bits/regex_compiler.tcc b/libstdc++-v3/include/bits/regex_compiler.tcc index 3667c860690..243b4a5cdbb 100644 --- a/libstdc++-v3/include/bits/regex_compiler.tcc +++ b/libstdc++-v3/include/bits/regex_compiler.tcc @@ -65,15 +65,7 @@ namespace __detail _Compiler<_TraitsT>:: _Compiler(_IterT __b, _IterT __e, const typename _TraitsT::locale_type& __loc, _FlagT __flags) - : _M_flags((__flags - & (regex_constants::ECMAScript - | regex_constants::basic - | regex_constants::extended - | regex_constants::grep - | regex_constants::egrep - | regex_constants::awk)) - ? __flags - : __flags | regex_constants::ECMAScript), + : _M_flags(_S_validate(__flags)), _M_scanner(__b, __e, _M_flags, __loc), _M_nfa(make_shared<_RegexT>(__loc, _M_flags)), _M_traits(_M_nfa->_M_traits), diff --git a/libstdc++-v3/include/bits/regex_error.h b/libstdc++-v3/include/bits/regex_error.h index f2899174352..b63d6572961 100644 --- a/libstdc++-v3/include/bits/regex_error.h +++ b/libstdc++-v3/include/bits/regex_error.h @@ -61,7 +61,8 @@ namespace regex_constants _S_error_badrepeat, _S_error_complexity, _S_error_stack, - _S_null + _S_null, + _S_grammar }; /** The expression contained an invalid collating element name. */ diff --git a/libstdc++-v3/testsuite/28_regex/basic_regex/ctors/grammar.cc b/libstdc++-v3/testsuite/28_regex/basic_regex/ctors/grammar.cc new file mode 100644 index 00000000000..fd8531c4530 --- /dev/null +++ b/libstdc++-v3/testsuite/28_regex/basic_regex/ctors/grammar.cc @@ -0,0 +1,53 @@ +// { dg-do run { target c++11 } } +#include +#include + +void +test01() +{ + std::regex re{""}; + VERIFY( re.flags() & std::regex::ECMAScript ); + + std::regex re2{"", std::regex::flag_type{}}; + VERIFY( re2.flags() == std::regex::flag_type() ); // See also PR 83598 +} + +void +test02() +{ + // A valid value of type syntax_option_type shall have at most one of the + // grammar elements ECMAScript, basic, extended, awk, grep, egrep, set. + + try + { + std::regex{"", std::regex::ECMAScript|std::regex::basic}; + VERIFY( false ); + } + catch (const std::regex_error&) + { + } + + try + { + std::regex{"", std::regex::extended|std::regex::basic}; + VERIFY( false ); + } + catch (const std::regex_error&) + { + } + + try + { + std::regex{"", std::regex::grep|std::regex::basic}; + VERIFY( false ); + } + catch (const std::regex_error&) + { + } +} + +int main() +{ + test01(); + test02(); +}