[gcc r14-11509] libstdc++: Fix -Wstringop-overread warning in std::vector<bool> [PR114758]
Jonathan Wakely
redi@gcc.gnu.org
Wed Apr 2 09:03:31 GMT 2025
https://gcc.gnu.org/g:f0c4ffcd86e50e7758f2408926bc880d3b810895
commit r14-11509-gf0c4ffcd86e50e7758f2408926bc880d3b810895
Author: Jonathan Wakely <jwakely@redhat.com>
Date: Fri Mar 28 15:41:41 2025 +0000
libstdc++: Fix -Wstringop-overread warning in std::vector<bool> [PR114758]
As in r13-4393-gcca06f0d6d76b0 and a few other commits, we can avoid
bogus warnings in std::vector<bool> by hoisting some loads to before the
allocation that calls operator new. This means that the compiler has
enough info to remove the dead branches that trigger bogus warnings.
On trunk this is only needed with -fno-assume-sane-operators-new-delete
but it will help on the branches where that option doesn't exist.
libstdc++-v3/ChangeLog:
PR libstdc++/114758
* include/bits/vector.tcc (vector<bool, A>::_M_fill_insert):
Hoist loads of begin() and end() before allocation.
* testsuite/23_containers/vector/bool/capacity/114758.cc: New
test.
Reviewed-by: Tomasz Kamiński <tkaminsk@redhat.com>
(cherry picked from commit 1f6c19f307c8de9830130a0ba071c24e3835beb3)
Diff:
---
libstdc++-v3/include/bits/vector.tcc | 5 +++--
.../testsuite/23_containers/vector/bool/capacity/114758.cc | 12 ++++++++++++
2 files changed, 15 insertions(+), 2 deletions(-)
diff --git a/libstdc++-v3/include/bits/vector.tcc b/libstdc++-v3/include/bits/vector.tcc
index 200a75a19c3e..dafc2a31a8b4 100644
--- a/libstdc++-v3/include/bits/vector.tcc
+++ b/libstdc++-v3/include/bits/vector.tcc
@@ -1087,11 +1087,12 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
{
const size_type __len =
_M_check_len(__n, "vector<bool>::_M_fill_insert");
+ 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);
std::fill(__i, __i + difference_type(__n), __x);
- iterator __finish = std::copy(__position, end(),
+ iterator __finish = std::copy(__position, __end,
__i + difference_type(__n));
this->_M_deallocate();
this->_M_impl._M_end_of_storage = __q + _S_nword(__len);
diff --git a/libstdc++-v3/testsuite/23_containers/vector/bool/capacity/114758.cc b/libstdc++-v3/testsuite/23_containers/vector/bool/capacity/114758.cc
new file mode 100644
index 000000000000..43efdef7e1d2
--- /dev/null
+++ b/libstdc++-v3/testsuite/23_containers/vector/bool/capacity/114758.cc
@@ -0,0 +1,12 @@
+// { dg-options "-O3 -Werror=stringop-overread" }
+// { dg-do compile }
+
+// Bug libstdc++/114758 The layout of a std::vector<bool> reports a warning
+
+#include <vector>
+
+void pr114758(std::vector<bool>& v)
+{
+ v.resize(3);
+ v = std::vector<bool>(3, false);
+}
More information about the Libstdc++-cvs
mailing list