diff --git a/libstdc++-v3/include/bits/vector.tcc b/libstdc++-v3/include/bits/vector.tcc index 29aa63e4742..7a92f34ec64 100644 --- a/libstdc++-v3/include/bits/vector.tcc +++ b/libstdc++-v3/include/bits/vector.tcc @@ -1106,9 +1106,12 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER vector:: _M_reallocate(size_type __n) { + const iterator __begin = begin(), __end = end(); + if (size_type(__end - __begin) > __n) + __builtin_unreachable(); _Bit_pointer __q = this->_M_allocate(__n); iterator __start(std::__addressof(*__q), 0); - iterator __finish(_M_copy_aligned(begin(), end(), __start)); + iterator __finish(_M_copy_aligned(__begin, __end, __start)); this->_M_deallocate(); this->_M_impl._M_start = __start; this->_M_impl._M_finish = __finish; diff --git a/libstdc++-v3/testsuite/23_containers/vector/bool/capacity/110498.cc b/libstdc++-v3/testsuite/23_containers/vector/bool/capacity/110498.cc new file mode 100644 index 00000000000..f848edcb25e --- /dev/null +++ b/libstdc++-v3/testsuite/23_containers/vector/bool/capacity/110498.cc @@ -0,0 +1,18 @@ +// { dg-options "-O3 -Werror=array-bounds -fno-assume-sane-operators-new-delete" } +// { dg-do compile } + +// Bug libstdc++/110498 +// Spurious warnings stringop-overflow and array-bounds copying data as bytes +// into vector::reserve + +#include + +void f(std::vector& v) +{ + // Warning emitted when set to any number in the range [1,64]. + const std::size_t reserve_size = 30; + + v.reserve(reserve_size); + v.push_back(0); +} +