From patchwork Wed Dec 10 01:37:45 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 419358 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 379011400D5 for ; Wed, 10 Dec 2014 12:38:16 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:subject:message-id:mime-version:content-type; q=dns; s= default; b=Q5VZNBeDqpUk9A0aYNnH4ZjaMrO72kfN2k13qzKBmUrIyQ4G+lNUb COgj6g9e32GojI4dnVxfLciqTgUdIyegDd3zz4kucm8qlcwg2fi5B8Ks8r3aYOru vGd5x3RG7ztVMr+7Sk4l0soUn48fQc7kdqFi46bo5ZIcCTQNkzCLIQ= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:subject:message-id:mime-version:content-type; s= default; bh=HUqsofUB7S/LpdW5b25bWYrpEpo=; b=pJ/Zd+fmdfi3DTln6e+3 itYzOA0/zU9cpVdNN+6R7ubpf7nQGAdD9eYb9SD8n7msMY+DkI87Jb+g+AxsC0zu xd+b5Wp4XwRZz+OPC88ApWlhw32kAQf2pvpBRGqjLq3Mfyl0sCENZ+EkztVbp6sW LrlWnlp3uOsNHCV9yqz3PDU= Received: (qmail 6836 invoked by alias); 10 Dec 2014 01:37:58 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 6808 invoked by uid 89); 10 Dec 2014 01:37:50 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.0 required=5.0 tests=AWL, BAYES_00, SPF_HELO_PASS, SPF_PASS, T_RP_MATCHES_RCVD autolearn=ham version=3.3.2 X-Spam-User: qpsmtpd, 2 recipients X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-GCM-SHA384 encrypted) ESMTPS; Wed, 10 Dec 2014 01:37:47 +0000 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id sBA1bkmq015423 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL); Tue, 9 Dec 2014 20:37:46 -0500 Received: from localhost (ovpn-116-46.ams2.redhat.com [10.36.116.46]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id sBA1bjEK004380; Tue, 9 Dec 2014 20:37:46 -0500 Date: Wed, 10 Dec 2014 01:37:45 +0000 From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [patch] std::regex: Implement LWG DR 2329 and DR 2332 Message-ID: <20141210013745.GJ3134@redhat.com> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.23 (2014-03-12) Two defect reports that protect against dangerous uses of std::regex involving dangling references to temporaries: http://cplusplus.github.io/LWG/lwg-defects.html#2329 http://cplusplus.github.io/LWG/lwg-defects.html#2332 Tested x86_64-linux & powerpc64-linux, committed to trunk. commit 616ac2600bcb71c505fa6286084a166500a8bf66 Author: Jonathan Wakely Date: Wed Dec 10 01:20:42 2014 +0000 Implement LWG DR 2329 and DR 2332. * include/bits/regex.h (regex_match, regex_search): LWG DR 2329, add deleted overloads for rvalue strings. (regex_iterator, regex_token_iterator): LWG DR 2332, add deleted constructors for rvalue regexes. * testsuite/28_regex/algorithms/regex_match/dr2329_neg.cc: New. * testsuite/28_regex/algorithms/regex_search/dr2329_neg.cc: New. * testsuite/28_regex/iterators/regex_iterator/ctors/char/dr2332_neg.cc: New. * testsuite/28_regex/iterators/regex_token_iterator/ctors/char/ dr2332_neg.cc: New. diff --git a/libstdc++-v3/include/bits/regex.h b/libstdc++-v3/include/bits/regex.h index 6ff9a82..cb6bc93 100644 --- a/libstdc++-v3/include/bits/regex.h +++ b/libstdc++-v3/include/bits/regex.h @@ -2063,6 +2063,19 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION = regex_constants::match_default) { return regex_match(__s.begin(), __s.end(), __m, __re, __flags); } + // _GLIBCXX_RESOLVE_LIB_DEFECTS + // 2329. regex_match() with match_results should forbid temporary strings + /// Prevent unsafe attempts to get match_results from a temporary string. + template + bool + regex_match(const basic_string<_Ch_type, _Ch_traits, _Ch_alloc>&&, + match_results::const_iterator, _Alloc>&, + const basic_regex<_Ch_type, _Rx_traits>&, + regex_constants::match_flag_type + = regex_constants::match_default) = delete; + /** * @brief Indicates if there is a match between the regular expression @p e * and a C-style null-terminated string. @@ -2239,6 +2252,20 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION = regex_constants::match_default) { return regex_search(__s.begin(), __s.end(), __m, __e, __f); } + // _GLIBCXX_RESOLVE_LIB_DEFECTS + // 2329. regex_search() with match_results should forbid temporary strings + /// Prevent unsafe attempts to get match_results from a temporary string. + template + bool + regex_search(const basic_string<_Ch_type, _Ch_traits, _Ch_alloc>&&, + match_results::const_iterator, _Alloc>&, + const basic_regex<_Ch_type, _Rx_traits>&, + regex_constants::match_flag_type + = regex_constants::match_default) = delete; + // std [28.11.4] Function template regex_replace /** * @brief Search for a regular expression within a range for multiple times, @@ -2437,6 +2464,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION *this = regex_iterator(); } + // _GLIBCXX_RESOLVE_LIB_DEFECTS + // 2332. regex_iterator should forbid temporary regexes + regex_iterator(_Bi_iter, _Bi_iter, const regex_type&&, + regex_constants::match_flag_type + = regex_constants::match_default) = delete; /** * Copy constructs a %regex_iterator. */ @@ -2618,6 +2650,25 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION _M_subs(__submatches, *(&__submatches+1)), _M_n(0) { _M_init(__a, __b); } + // _GLIBCXX_RESOLVE_LIB_DEFECTS + // 2332. regex_token_iterator should forbid temporary regexes + regex_token_iterator(_Bi_iter, _Bi_iter, const regex_type&&, int = 0, + regex_constants::match_flag_type = + regex_constants::match_default) = delete; + regex_token_iterator(_Bi_iter, _Bi_iter, const regex_type&&, + const std::vector&, + regex_constants::match_flag_type = + regex_constants::match_default) = delete; + regex_token_iterator(_Bi_iter, _Bi_iter, const regex_type&&, + initializer_list, + regex_constants::match_flag_type = + regex_constants::match_default) = delete; + template + regex_token_iterator(_Bi_iter, _Bi_iter, const regex_type&&, + const int (&)[N], + regex_constants::match_flag_type = + regex_constants::match_default) = delete; + /** * @brief Copy constructs a %regex_token_iterator. * @param __rhs [IN] A %regex_token_iterator to copy. diff --git a/libstdc++-v3/testsuite/28_regex/algorithms/regex_match/dr2329_neg.cc b/libstdc++-v3/testsuite/28_regex/algorithms/regex_match/dr2329_neg.cc new file mode 100644 index 0000000..3070f93 --- /dev/null +++ b/libstdc++-v3/testsuite/28_regex/algorithms/regex_match/dr2329_neg.cc @@ -0,0 +1,28 @@ +// Copyright (C) 2014 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +// { dg-options "-std=gnu++11" } +// { dg-do compile } + +#include + +void +test01() +{ + std::smatch m; + regex_match(std::string{}, m, std::regex{"."}); // { dg-error "deleted" } +} diff --git a/libstdc++-v3/testsuite/28_regex/algorithms/regex_search/dr2332_neg.cc b/libstdc++-v3/testsuite/28_regex/algorithms/regex_search/dr2332_neg.cc new file mode 100644 index 0000000..2381ddb --- /dev/null +++ b/libstdc++-v3/testsuite/28_regex/algorithms/regex_search/dr2332_neg.cc @@ -0,0 +1,28 @@ +// Copyright (C) 2014 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +// { dg-options "-std=gnu++11" } +// { dg-do compile } + +#include + +void +test01() +{ + std::smatch m; + regex_search(std::string{}, m, std::regex{"."}); // { dg-error "deleted" } +} diff --git a/libstdc++-v3/testsuite/28_regex/iterators/regex_iterator/ctors/char/dr2332_neg.cc b/libstdc++-v3/testsuite/28_regex/iterators/regex_iterator/ctors/char/dr2332_neg.cc new file mode 100644 index 0000000..2a72b4f --- /dev/null +++ b/libstdc++-v3/testsuite/28_regex/iterators/regex_iterator/ctors/char/dr2332_neg.cc @@ -0,0 +1,29 @@ +// Copyright (C) 2014 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +// { dg-options "-std=gnu++11" } +// { dg-do compile } + +#include + +void +test01() +{ + using iter_type = std::regex_iterator; + const char* s = ""; + iter_type(s, s, std::regex{}); // { dg-error "deleted" } +} diff --git a/libstdc++-v3/testsuite/28_regex/iterators/regex_token_iterator/ctors/char/dr2332_neg.cc b/libstdc++-v3/testsuite/28_regex/iterators/regex_token_iterator/ctors/char/dr2332_neg.cc new file mode 100644 index 0000000..6ff5870 --- /dev/null +++ b/libstdc++-v3/testsuite/28_regex/iterators/regex_token_iterator/ctors/char/dr2332_neg.cc @@ -0,0 +1,39 @@ +// Copyright (C) 2014 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +// { dg-options "-std=gnu++11" } +// { dg-do compile } + +#include + +void +test01() +{ + using iter_type = std::regex_token_iterator; + const char* s = ""; + + iter_type(s, s, std::regex{}); // { dg-error "deleted" } + + std::vector v; + iter_type(s, s, std::regex{}, v); // { dg-error "deleted" } + + std::initializer_list il = {1}; + iter_type(s, s, std::regex{}, il); // { dg-error "deleted" } + + const int i[2] = { }; + iter_type(s, s, std::regex{}, i); // { dg-error "deleted" } +}