From patchwork Thu Dec 11 08:27:04 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tim Shen X-Patchwork-Id: 419980 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 8C49E140081 for ; Thu, 11 Dec 2014 19:27:35 +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 :mime-version:date:message-id:subject:from:to:content-type; q= dns; s=default; b=PmHGWN01bI5teCKWCRFYlrXIl+RdhmYPerarY8i8Grg3a+ pmbQBqWc2DxvTDawh77feitYB9PG4/zA6+mE8fW3UIJ820YBZMc1MpXVAtKbS1Hq Kx/9Qvbt2FeqgMEsB3mm2RuzHIl3QBaWTc9L2L/BRsNzT6+S0medD7rMgXBJ0= 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 :mime-version:date:message-id:subject:from:to:content-type; s= default; bh=mOmpqxFP2ltIA5av/XQO3LXhymA=; b=sQ88nQgkH0BJPGAbkwbm Fv5hYIeaBDwQGnnIqtHRN47Fq/nl5cbBihLdBTzmrfn8XS6eVEj6vfXShmVuhp8K gh8fbEfsZDxF005jzMj5pDTxSZxKiW+TAoyIjPqIU6Ht9E4uvzAIRhskuAo5qJXL bxEbYP1xor9PzUUIAqgjBi4= Received: (qmail 20858 invoked by alias); 11 Dec 2014 08:27:14 -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 20706 invoked by uid 89); 11 Dec 2014 08:27:12 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.5 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_LOW, SPF_PASS, T_RP_MATCHES_RCVD autolearn=unavailable version=3.3.2 X-HELO: mail-qg0-f51.google.com Received: from mail-qg0-f51.google.com (HELO mail-qg0-f51.google.com) (209.85.192.51) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-SHA encrypted) ESMTPS; Thu, 11 Dec 2014 08:27:07 +0000 Received: by mail-qg0-f51.google.com with SMTP id e89so3448717qgf.10 for ; Thu, 11 Dec 2014 00:27:05 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:date:message-id:subject:from:to :content-type; bh=QgAXYjJRwKclQgV8+4Y0QvrbhLbLxEelNYP5UvLtUEU=; b=TiYpxo0MnChE/G0+nxlc4nYDeuJq8ba3RgySb8ebt8vrP8cJ9d0MXwGIOb9fGEIaMn M+ctIiM+SVaRaCCVqoXDQWK11QgdbdOxrmSQbaNVvE9xi8tJGghUQjn+f+ZqnnZiFANU b/XHcgTlTSvV7MQy71GM10oEvPxc+HanQsdnULTnGOjMksoFZR46mEQv4Mz+GOhlWKM6 kCku5xodaVqrtTAfSRz4U88xyolChSGyUjeWGaMSGovYEAoYcn75VdO1oLu++ae/b5Yq 46OY5Q9zYxYOtSc2ijKt4KgoxKQW1G3pTnF+oSFdNJXCjUyhsTpDLJuZKco5sl/ZtyQc tLag== X-Gm-Message-State: ALoCoQnGwIhtfgHCP/l0iMwHPOOPum1AlP+pTPDNXAnvzdgUQv2ktE55p7cDCDo/D7HFH+IG6u/W MIME-Version: 1.0 X-Received: by 10.140.101.204 with SMTP id u70mr4649315qge.0.1418286425053; Thu, 11 Dec 2014 00:27:05 -0800 (PST) Received: by 10.96.254.231 with HTTP; Thu, 11 Dec 2014 00:27:04 -0800 (PST) Date: Thu, 11 Dec 2014 00:27:04 -0800 Message-ID: Subject: [Patch, libstdc++/64239] Fix regex_iterator copying From: Tim Shen To: "libstdc++" , gcc-patches As discussed in Bugzilla. Bootstrapped and tested. Is it Ok to backport it to 4.9 branch, with _M_in_iterator kept unused? Thanks! :) diff --git a/libstdc++-v3/include/bits/regex.h b/libstdc++-v3/include/bits/regex.h index cb6bc93..3afec37 100644 --- a/libstdc++-v3/include/bits/regex.h +++ b/libstdc++-v3/include/bits/regex.h @@ -1563,42 +1563,30 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION */ explicit match_results(const _Alloc& __a = _Alloc()) - : _Base_type(__a), _M_in_iterator(false) + : _Base_type(__a) { } /** * @brief Copy constructs a %match_results. */ - match_results(const match_results& __rhs) - : _Base_type(__rhs), _M_in_iterator(false) - { } + match_results(const match_results& __rhs) = default; /** * @brief Move constructs a %match_results. */ - match_results(match_results&& __rhs) noexcept - : _Base_type(std::move(__rhs)), _M_in_iterator(false) - { } + match_results(match_results&& __rhs) noexcept = default; /** * @brief Assigns rhs to *this. */ match_results& - operator=(const match_results& __rhs) - { - match_results(__rhs).swap(*this); - return *this; - } + operator=(const match_results& __rhs) = default; /** * @brief Move-assigns rhs to *this. */ match_results& - operator=(match_results&& __rhs) - { - match_results(std::move(__rhs)).swap(*this); - return *this; - } + operator=(match_results&& __rhs) = default; /** * @brief Destroys a %match_results object. @@ -1685,13 +1673,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION difference_type position(size_type __sub = 0) const { - // [28.12.1.4.5] - if (_M_in_iterator) - return __sub < size() ? std::distance(_M_begin, - (*this)[__sub].first) : -1; - else - return __sub < size() ? std::distance(this->prefix().first, - (*this)[__sub].first) : -1; + return __sub < size() ? std::distance(_M_begin, + (*this)[__sub].first) : -1; } /** @@ -1876,7 +1859,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION */ void swap(match_results& __that) - { _Base_type::swap(__that); } + { + _Base_type::swap(__that); + swap(_M_begin, __that._M_begin); + } //@} private: @@ -1894,7 +1880,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION regex_constants::match_flag_type); _Bi_iter _M_begin; - bool _M_in_iterator; }; typedef match_results cmatch; diff --git a/libstdc++-v3/include/bits/regex.tcc b/libstdc++-v3/include/bits/regex.tcc index 9692402..b676428 100644 --- a/libstdc++-v3/include/bits/regex.tcc +++ b/libstdc++-v3/include/bits/regex.tcc @@ -62,6 +62,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION return false; typename match_results<_BiIter, _Alloc>::_Base_type& __res = __m; + __m._M_begin = __s; __res.resize(__re._M_automaton->_M_sub_count() + 2); for (auto& __it : __res) __it.matched = false; @@ -572,7 +573,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION auto& __prefix = _M_match.at(_M_match.size()); __prefix.first = __prefix_first; __prefix.matched = __prefix.first != __prefix.second; - _M_match._M_in_iterator = true; + // [28.12.1.4.5] _M_match._M_begin = _M_begin; return *this; } @@ -587,7 +588,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION auto& __prefix = _M_match.at(_M_match.size()); __prefix.first = __prefix_first; __prefix.matched = __prefix.first != __prefix.second; - _M_match._M_in_iterator = true; + // [28.12.1.4.5] _M_match._M_begin = _M_begin; } else diff --git a/libstdc++-v3/testsuite/28_regex/iterators/regex_iterator/char/string_position_01.cc b/libstdc++-v3/testsuite/28_regex/iterators/regex_iterator/char/string_position_01.cc index 5fa4ea7..91aa061 100644 --- a/libstdc++-v3/testsuite/28_regex/iterators/regex_iterator/char/string_position_01.cc +++ b/libstdc++-v3/testsuite/28_regex/iterators/regex_iterator/char/string_position_01.cc @@ -24,6 +24,7 @@ // Tests iter->position() behavior #include +#include #include void @@ -41,9 +42,53 @@ test01() } } +// PR libstdc++/64239 +void +test02() +{ + bool test __attribute__((unused)) = true; + + std::regex re("\\w+"); + std::string s("-a-b-c-"); + + std::tuple expected[] = + { + std::make_tuple(1, 1, "a"), + std::make_tuple(3, 1, "b"), + std::make_tuple(5, 1, "c"), + }; + + int i = 0; + for (auto it1 = std::sregex_iterator(s.begin(), s.end(), re), + end = std::sregex_iterator(); it1 != end; ++it1, i++) + { + auto it2 = it1; + VERIFY(it1->position() == std::get<0>(expected[i])); + VERIFY(it1->length() == std::get<1>(expected[i])); + VERIFY(it1->str() == std::get<2>(expected[i])); + VERIFY(it2->position() == std::get<0>(expected[i])); + VERIFY(it2->length() == std::get<1>(expected[i])); + VERIFY(it2->str() == std::get<2>(expected[i])); + } +} + +void +test03() +{ + bool test __attribute__((unused)) = true; + + std::smatch m; + std::string s = "abcde"; + std::regex_search(s, m, std::regex("bcd")); + VERIFY(m.position() == 1); + VERIFY(m.position() == m.prefix().length()); +} + int main() { test01(); + test02(); + test03(); return 0; }