From patchwork Wed Sep 29 13:16:11 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 45551 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 927153857C40 for ; Wed, 29 Sep 2021 13:19:47 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 927153857C40 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1632921587; bh=4PMoc6tNefSEvtxnZn/uBEHpPt0GXlUHiBaZjQFT9AU=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=yfv+GCNi4ok/gKWNHuRRlXfOiK80WfkaNrKtkFY6Br3UtpvvxOlUILAhDIdDUNFMG Raz9LwfiZta3hFba2PPIHJVYzmoGZ9UvycJMgVQbXZT7wGWpGJhnUTyK/zV7gU8Vbn ysSYTNzzgQotJheE9OGIa19McLPuwEnaQ1Ccht4U= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTP id BE3B0385803B for ; Wed, 29 Sep 2021 13:16:15 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org BE3B0385803B Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-346-NckaiMNHMWuYAdlCgDw2KA-1; Wed, 29 Sep 2021 09:16:13 -0400 X-MC-Unique: NckaiMNHMWuYAdlCgDw2KA-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id F27FE1922964; Wed, 29 Sep 2021 13:16:12 +0000 (UTC) Received: from localhost (unknown [10.33.36.241]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9F2A919C59; Wed, 29 Sep 2021 13:16:12 +0000 (UTC) Date: Wed, 29 Sep 2021 14:16:11 +0100 To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Check for invalid syntax_option_type values in Message-ID: MIME-Version: 1.0 X-Clacks-Overhead: GNU Terry Pratchett X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-13.8 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jonathan Wakely via Gcc-patches From: Jonathan Wakely Reply-To: Jonathan Wakely Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" 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. Tested x86_64-linux. Committed to trunk. commit 9ca4c42a3b756e54a92ff8e1ac6c396b680b7839 Author: Jonathan Wakely Date: Wed Sep 29 13:48:15 2021 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. diff --git a/libstdc++-v3/include/bits/regex_compiler.h b/libstdc++-v3/include/bits/regex_compiler.h index 898607d81c6..62a49bf52cf 100644 --- a/libstdc++-v3/include/bits/regex_compiler.h +++ b/libstdc++-v3/include/bits/regex_compiler.h @@ -143,6 +143,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 1bd30972cbb..956262a12c9 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(const _CharT* __b, const _CharT* __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 722ce26cda3..e7b7b420ec4 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(); +}