From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id B709B385AF83; Fri, 28 Jul 2023 17:32:20 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B709B385AF83 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1690565540; bh=3Pr3FnVJupqWZMTwR5XHGvemwSVHPJV0zCAFBf4BvAE=; h=From:To:Subject:Date:From; b=rFgdAPWQ0wPCqskDOLaK6uNIy2kCRmzPXyklgj5no5NU2/Ar3tU268s+sJe4dLXcw 3PjvwhCQxAyzaGYoQVKFnYYgRm7wlMLND3c+EygG8wpDGpVORKKbmKywXXHI+v2+LU 9mvivswUkRKzLm5lnO6LsyIgyooBCSkyNNZrsB4M= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jonathan Wakely To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r13-7625] libstdc++: Avoid bogus overflow warnings in std::vector [PR110807] X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/releases/gcc-13 X-Git-Oldrev: 3f78294969641cb762dc2abcd832c7d3edeefa79 X-Git-Newrev: 962cd3e2c475e8197fd0cfa7330a8f352b4ff5b2 Message-Id: <20230728173220.B709B385AF83@sourceware.org> Date: Fri, 28 Jul 2023 17:32:20 +0000 (GMT) List-Id: https://gcc.gnu.org/g:962cd3e2c475e8197fd0cfa7330a8f352b4ff5b2 commit r13-7625-g962cd3e2c475e8197fd0cfa7330a8f352b4ff5b2 Author: Jonathan Wakely Date: Wed Jul 26 14:09:24 2023 +0100 libstdc++: Avoid bogus overflow warnings in std::vector [PR110807] 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. (cherry picked from commit 7931a1de9ec87b996d51d3d60786f5c81f63919f) Diff: --- libstdc++-v3/include/bits/stl_bvector.h | 3 ++- libstdc++-v3/include/bits/vector.tcc | 5 +++-- libstdc++-v3/testsuite/23_containers/vector/bool/110807.cc | 14 ++++++++++++++ 3 files changed, 19 insertions(+), 3 deletions(-) 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 d6fdea2dd01..c762dbf798f 100644 --- a/libstdc++-v3/include/bits/vector.tcc +++ b/libstdc++-v3/include/bits/vector.tcc @@ -910,11 +910,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..2e9d4019edb --- /dev/null +++ b/libstdc++-v3/testsuite/23_containers/vector/bool/110807.cc @@ -0,0 +1,14 @@ +// { dg-options "-O2" } +// { dg-do compile { target c++11 } } + +// Bug 110807 +// Copy list initialisation of a vector raises a warning with -O2 + +#include + +std::vector byCallSpread; + +void f() +{ + byCallSpread = {true}; +}