From patchwork Wed Jul 26 16:03:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 126481 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:a985:0:b0:3e4:2afc:c1 with SMTP id t5csp502436vqo; Wed, 26 Jul 2023 09:05:32 -0700 (PDT) X-Google-Smtp-Source: APBJJlEZgGvxw5k+Z0wminDIV2mQhHgUSVv5toc1RLBVgBr2HAs9+V3ZDu0HNw6gpAmqosccmtHp X-Received: by 2002:aa7:d291:0:b0:522:586d:6f3a with SMTP id w17-20020aa7d291000000b00522586d6f3amr1955118edq.6.1690387531872; Wed, 26 Jul 2023 09:05:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690387531; cv=none; d=google.com; s=arc-20160816; b=v36bRjl/xuqHa2YrB96pQoUl6eKt3PBvXarD7u9snmaOWGlXAy6ZWHOiFP2XPRbVwP z2uehlPNoZ2H+C9xdx85lvIV5O/GkXoO25G05lNpYNSvbjPP5zJWhAAejcB2EUyemgi2 I2NxYOJ1YVdvam7i0pOuAO9/pbWL4E5jKAkxpYLCaelnYDxQK+l+FVGPrdiOeN7Ip6Y2 D9yzjMhMLNbs/781xU+p9XBgFiB1JCAlcZ/4cbY7w98f/z6UdnNUoTprhrDYr0maJG5H by2XomaJIjku2JtTBHUnQdLBSDRSPwXkT0+VN4a9jqwXW1/2vpcmbDeNlrSYiEPqwT1i XkNA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=wWA0LS707glfYa6NLJ5DqdIKbIHQNn+SpaTgJZwAjNQ=; fh=sC8lnOfyH6gL5FYlMbZ1VDz6qVPMj/3ViLJWKNUCUgQ=; b=Mh3gmNwYzZZLTiV+zb2wJkVCiAbF0F3jJgq2wLvIjC4AFf/iqAIWyuNrRXe7fnH6BO ifHvLdEag4+ml7yj0eF7fykBesKe8apa/f5PQo3s+D04JuTrWVDuZb3i1gaCienRTHqf pOJ5+zoEBYKXXuw1RjKTczRIpQWNrlL9KBvIWj/XR9SsYjoaZKMDoHBVhCCAVurWxa5E +mkxtoOm3batmEZuc0wKlZ3Bg6Vy/e88CyBgK91wfCM5tRQ4nR/W/YM0KhPGlAMiRVR9 VVnB6xZE7eVSOOx34Mq5VMuGMcSdBxLoFKW+yWdgpwxZ6V+znceiVcCn8dX0QaZS91A4 Lq6g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=sjnfnxLA; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id d28-20020a056402517c00b0052228551730si5651027ede.279.2023.07.26.09.05.31 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 26 Jul 2023 09:05:31 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=sjnfnxLA; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 11E5F3870C1D for ; Wed, 26 Jul 2023 16:04:38 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 11E5F3870C1D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1690387478; bh=wWA0LS707glfYa6NLJ5DqdIKbIHQNn+SpaTgJZwAjNQ=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=sjnfnxLAcsxACi1xfW1+G3BRF06J+kN6dZQH/ezYEjbF9Z8PLRN//dcYu0VIXwjmS BpVy99QyYSqW73Gcq0sDR0RbBhGS8HnWqklWUkBV0zUBR5F55o0NRfYGZaQOZvFv8H OWz5F/GCSzbC+wKly/xIdQoDBBWa6fRBujcJir1s= 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.129.124]) by sourceware.org (Postfix) with ESMTPS id 3E07B385C6EA for ; Wed, 26 Jul 2023 16:03:51 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 3E07B385C6EA Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-659-FRv8BBxKOcyMFVZenMEoUg-1; Wed, 26 Jul 2023 12:03:45 -0400 X-MC-Unique: FRv8BBxKOcyMFVZenMEoUg-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 619A385A58A; Wed, 26 Jul 2023 16:03:45 +0000 (UTC) Received: from localhost (unknown [10.42.28.198]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2701B40C206F; Wed, 26 Jul 2023 16:03:45 +0000 (UTC) To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Avoid bogus overflow warnings in std::vector [PR110807] Date: Wed, 26 Jul 2023 17:03:21 +0100 Message-ID: <20230726160344.220228-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.7 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_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE, URI_HEX autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) 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+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1772499796532736314 X-GMAIL-MSGID: 1772499796532736314 Tested x86_64-linux. Pushed to trunk. Backport to gcc-13 will come after the 13.2 release. -- >8 -- GCC thinks the allocation can alter the object being copied if it's globally reachable, so doesn't realize that [x.begin(), x.end()) after the allocation is the same as x.size() before it. This causes a testsuite failure when testing with -D_GLIBCXX_DEBUG: FAIL: 23_containers/vector/bool/swap.cc (test for excess errors) A fix is to move the calls to x.begin() and x.end() to before the allocation. A similar problem exists in vector::_M_insert_range where *this is globally reachable, as reported in PR libstdc++/110807. That can also be fixed by moving calls to begin() and end() before the allocation. libstdc++-v3/ChangeLog: PR libstdc++/110807 * include/bits/stl_bvector.h (vector(const vector&)): Access iterators before allocating. * include/bits/vector.tcc (vector::_M_insert_range): Likewise. * testsuite/23_containers/vector/bool/110807.cc: New test. --- libstdc++-v3/include/bits/stl_bvector.h | 3 ++- libstdc++-v3/include/bits/vector.tcc | 5 +++-- .../testsuite/23_containers/vector/bool/110807.cc | 14 ++++++++++++++ 3 files changed, 19 insertions(+), 3 deletions(-) create mode 100644 libstdc++-v3/testsuite/23_containers/vector/bool/110807.cc diff --git a/libstdc++-v3/include/bits/stl_bvector.h b/libstdc++-v3/include/bits/stl_bvector.h index ad462c5933c..8d18bcaffd4 100644 --- a/libstdc++-v3/include/bits/stl_bvector.h +++ b/libstdc++-v3/include/bits/stl_bvector.h @@ -773,8 +773,9 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER vector(const vector& __x) : _Base(_Bit_alloc_traits::_S_select_on_copy(__x._M_get_Bit_allocator())) { + const_iterator __xbegin = __x.begin(), __xend = __x.end(); _M_initialize(__x.size()); - _M_copy_aligned(__x.begin(), __x.end(), begin()); + _M_copy_aligned(__xbegin, __xend, begin()); } #if __cplusplus >= 201103L diff --git a/libstdc++-v3/include/bits/vector.tcc b/libstdc++-v3/include/bits/vector.tcc index f592c72dec2..ada396c9b30 100644 --- a/libstdc++-v3/include/bits/vector.tcc +++ b/libstdc++-v3/include/bits/vector.tcc @@ -980,11 +980,12 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER { const size_type __len = _M_check_len(__n, "vector::_M_insert_range"); + const iterator __begin = begin(), __end = end(); _Bit_pointer __q = this->_M_allocate(__len); iterator __start(std::__addressof(*__q), 0); - iterator __i = _M_copy_aligned(begin(), __position, __start); + iterator __i = _M_copy_aligned(__begin, __position, __start); __i = std::copy(__first, __last, __i); - iterator __finish = std::copy(__position, end(), __i); + iterator __finish = std::copy(__position, __end, __i); this->_M_deallocate(); this->_M_impl._M_end_of_storage = __q + _S_nword(__len); this->_M_impl._M_start = __start; diff --git a/libstdc++-v3/testsuite/23_containers/vector/bool/110807.cc b/libstdc++-v3/testsuite/23_containers/vector/bool/110807.cc new file mode 100644 index 00000000000..5c019bd9524 --- /dev/null +++ b/libstdc++-v3/testsuite/23_containers/vector/bool/110807.cc @@ -0,0 +1,14 @@ +// { dg-options "-O2" } +// { dg-do compile } + +// Bug 110807 +// Copy list initialisation of a vector raises a warning with -O2 + +#include + +std::vector byCallSpread; + +void f() +{ + byCallSpread = {true}; +}