]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Avoid bogus overflow warnings in std::vector<bool> [PR110807]
authorJonathan Wakely <jwakely@redhat.com>
Wed, 26 Jul 2023 13:09:24 +0000 (14:09 +0100)
committerJonathan Wakely <jwakely@redhat.com>
Wed, 26 Jul 2023 16:02:46 +0000 (17:02 +0100)
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<bool>::_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<bool>::_M_insert_range):
Likewise.
* testsuite/23_containers/vector/bool/110807.cc: New test.

libstdc++-v3/include/bits/stl_bvector.h
libstdc++-v3/include/bits/vector.tcc
libstdc++-v3/testsuite/23_containers/vector/bool/110807.cc [new file with mode: 0644]

index ad462c5933c2a660a04e4113622cf5fccc832ea2..8d18bcaffd4341b0cc6b1a093b8cc7baf8117c61 100644 (file)
@@ -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
index f592c72dec242664ce91a7a2fdf4ea0ed06d6a21..ada396c9b30aa6c548d57a393d63f1209370e109 100644 (file)
@@ -980,11 +980,12 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
              {
                const size_type __len =
                  _M_check_len(__n, "vector<bool>::_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 (file)
index 0000000..5c019bd
--- /dev/null
@@ -0,0 +1,14 @@
+// { dg-options "-O2" }
+// { dg-do compile }
+
+// Bug 110807
+// Copy list initialisation of a vector<bool> raises a warning with -O2
+
+#include <vector>
+
+std::vector<bool> byCallSpread;
+
+void f()
+{
+  byCallSpread = {true};
+}