From patchwork Tue Nov 25 08:41:27 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tim Shen X-Patchwork-Id: 414341 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 77076140180 for ; Tue, 25 Nov 2014 19:41:40 +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:in-reply-to:references:date:message-id:subject :from:to:cc:content-type; q=dns; s=default; b=YQ74xa9RVTNdT9MqZH dCIcokR8udATFzS1aJfePDjCjERn2z5VddP/BjvvwXCaUKDvX3SBBQRnWQB+lmTn WSydF8ljHXSpgzS7BXiGzQJQpiOuPHz9EcSba0sv14M4ZpVE3GC6XUVXfryjiFS3 dKx431Ei6G/9t+FiTb6/NCgok= 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:in-reply-to:references:date:message-id:subject :from:to:cc:content-type; s=default; bh=WGc6p6RkWY6fD7YiDu35gNy0 2fs=; b=roKKLtOJQ/vukvChvjMnatAEtE7x5o6iQuqUgnsNxl+4WD7BajheWbIy yQsoYZ0h2KagT+YuD/mJgPwnmNZbvCcbPK0GJ/uLoq68dggkQhl9USDF1NRjpr0a 2+V82XXMV81yWcvOAWpzE3NpZPRlJH6q7XwxGU5Y4E0nqE6KKJk= Received: (qmail 12834 invoked by alias); 25 Nov 2014 08:41:31 -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 12807 invoked by uid 89); 25 Nov 2014 08:41:31 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.6 required=5.0 tests=AWL, BAYES_20, RCVD_IN_DNSWL_LOW, SPF_PASS, T_RP_MATCHES_RCVD autolearn=ham version=3.3.2 X-HELO: mail-qa0-f41.google.com Received: from mail-qa0-f41.google.com (HELO mail-qa0-f41.google.com) (209.85.216.41) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-SHA encrypted) ESMTPS; Tue, 25 Nov 2014 08:41:29 +0000 Received: by mail-qa0-f41.google.com with SMTP id f12so71748qad.0 for ; Tue, 25 Nov 2014 00:41:27 -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:in-reply-to:references:date :message-id:subject:from:to:cc:content-type; bh=l/mjTxZFO1ysa1QZLGLArZqeifil+kdI0lRTCfizhBc=; b=BeCi0shYIAY+cB7eo2+RLupTc9o4DrS3PosuY2190ngDcByta1jt4UusdMEjmVOwJd Ez9KhRb19k60JSvFo306KlzxmEuwqHojp2BSfWD8kBdYRdxP87DnU7K3q6F/hDyVHy7z E+0tH/pbW+c7bBJjb/H5QeSZpmUmcxWDy6uM2XVfx9NXF3VpsDJQTRwG++00PS8dFsXi AF+FIxUNasujP4riQb89hdr8Z+T0D2OjbnvEjxnT/c1NedCzM6zWAlq4OfHH5pOcU9Rp KPhxb/8Jqh8VfPD7jonrJJt+MqBgZzeRvOAYdj26ybRYFYqmliRNd8XKSGKlceFBDBnN ZHSQ== X-Gm-Message-State: ALoCoQlrmDIOha3/mvzPZwpJ1ZiIxNawSQ1CBibOXiN1UYni+S+nfY4f0FnqwHLymrXb7nPbnTev MIME-Version: 1.0 X-Received: by 10.224.13.145 with SMTP id c17mr35781593qaa.96.1416904887669; Tue, 25 Nov 2014 00:41:27 -0800 (PST) Received: by 10.96.254.231 with HTTP; Tue, 25 Nov 2014 00:41:27 -0800 (PST) In-Reply-To: References: <20141021102545.GI3033@redhat.com> <20141022143426.GR3033@redhat.com> <20141022230405.GU3033@redhat.com> Date: Tue, 25 Nov 2014 00:41:27 -0800 Message-ID: Subject: Re: [Patch, libstdc++/63497] Avoid dereferencing invalid iterator in regex_executor From: Tim Shen To: Jonathan Wakely Cc: "libstdc++" , gcc-patches On Wed, Oct 22, 2014 at 8:19 PM, Tim Shen wrote: > Committed. Thank you too! I'm backporting this patch to gcc-4_9-branch. Do we usually boot & test it and then commit directly, or it should be reviewed again? diff --git a/libstdc++-v3/include/bits/regex_executor.h b/libstdc++-v3/include/bits/regex_executor.h index 708c78e..0d1b676 100644 --- a/libstdc++-v3/include/bits/regex_executor.h +++ b/libstdc++-v3/include/bits/regex_executor.h @@ -134,7 +134,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION } bool - _M_word_boundary(_State<_TraitsT> __state) const; + _M_word_boundary() const; bool _M_lookahead(_State<_TraitsT> __state); diff --git a/libstdc++-v3/include/bits/regex_executor.tcc b/libstdc++-v3/include/bits/regex_executor.tcc index 052302b..ef49161 100644 --- a/libstdc++-v3/include/bits/regex_executor.tcc +++ b/libstdc++-v3/include/bits/regex_executor.tcc @@ -257,7 +257,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION _M_dfs<__match_mode>(__state._M_next); break; case _S_opcode_word_boundary: - if (_M_word_boundary(__state) == !__state._M_neg) + if (_M_word_boundary() == !__state._M_neg) _M_dfs<__match_mode>(__state._M_next); break; // Here __state._M_alt offers a single start node for a sub-NFA. @@ -267,9 +267,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION _M_dfs<__match_mode>(__state._M_next); break; case _S_opcode_match: + if (_M_current == _M_end) + break; if (__dfs_mode) { - if (_M_current != _M_end && __state._M_matches(*_M_current)) + if (__state._M_matches(*_M_current)) { ++_M_current; _M_dfs<__match_mode>(__state._M_next); @@ -348,25 +350,26 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template bool _Executor<_BiIter, _Alloc, _TraitsT, __dfs_mode>:: - _M_word_boundary(_State<_TraitsT> __state) const + _M_word_boundary() const { - // By definition. - bool __ans = false; - auto __pre = _M_current; - --__pre; - if (!(_M_at_begin() && _M_at_end())) + bool __left_is_word = false; + if (_M_current != _M_begin + || (_M_flags & regex_constants::match_prev_avail)) { - if (_M_at_begin()) - __ans = _M_is_word(*_M_current) - && !(_M_flags & regex_constants::match_not_bow); - else if (_M_at_end()) - __ans = _M_is_word(*__pre) - && !(_M_flags & regex_constants::match_not_eow); - else - __ans = _M_is_word(*_M_current) - != _M_is_word(*__pre); + auto __prev = _M_current; + if (_M_is_word(*std::prev(__prev))) + __left_is_word = true; } - return __ans; + bool __right_is_word = + _M_current != _M_end && _M_is_word(*_M_current); + + if (__left_is_word == __right_is_word) + return false; + if (__left_is_word && !(_M_flags & regex_constants::match_not_eow)) + return true; + if (__right_is_word && !(_M_flags & regex_constants::match_not_bow)) + return true; + return false; } _GLIBCXX_END_NAMESPACE_VERSION