https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110060
Bug ID: 110060 Summary: Adding optimizer hints to std::vector causes a new -Wstringop-overread false positive Product: gcc Version: 14.0 Status: UNCONFIRMED Keywords: diagnostic Severity: normal Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: redi at gcc dot gnu.org Blocks: 97048 Target Milestone: --- Created attachment 55223 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=55223&action=edit Preprocessed source for FAIL 23_containers/vector/types/1.cc As discussed on IRC recently, I want to add optimization hints to std::vector::size() and std::vector::capacity() so that gcc knows there will be no reallocation here: vec.assign(vec.size(), 0); The call to assign will not reallocate because size() <= capacity() is always true. We can express that invariant in the code. However, doing so causes a new testsuite FAIL: FAIL: 23_containers/vector/types/1.cc (test for excess errors) $ ~/gcc/latest/bin/g++ -O2 -Wall a-1.ii In file included from /home/jwakely/gcc/14/include/c++/14.0.0/vector:62, from /home/jwakely/src/gcc/gcc/libstdc++-v3/testsuite/23_containers/vector/types/1.cc:23: In static member function ‘static _Up* std::__copy_move<_IsMove, true, std::random_access_iterator_tag>::__copy_m(_Tp*, _Tp*, _Up*) [with _Tp = greedy_ops::X; _Up = greedy_ops::X; bool _IsMove = false]’, inlined from ‘_OI std::__copy_move_a2(_II, _II, _OI) [with bool _IsMove = false; _II = greedy_ops::X*; _OI = greedy_ops::X*]’ at /home/jwakely/gcc/14/include/c++/14.0.0/bits/stl_algobase.h:506:30, inlined from ‘_OI std::__copy_move_a1(_II, _II, _OI) [with bool _IsMove = false; _II = greedy_ops::X*; _OI = greedy_ops::X*]’ at /home/jwakely/gcc/14/include/c++/14.0.0/bits/stl_algobase.h:533:42, inlined from ‘_OI std::__copy_move_a(_II, _II, _OI) [with bool _IsMove = false; _II = greedy_ops::X*; _OI = greedy_ops::X*]’ at /home/jwakely/gcc/14/include/c++/14.0.0/bits/stl_algobase.h:540:31, inlined from ‘_OI std::copy(_II, _II, _OI) [with _II = greedy_ops::X*; _OI = greedy_ops::X*]’ at /home/jwakely/gcc/14/include/c++/14.0.0/bits/stl_algobase.h:633:7, inlined from ‘std::vector<_Tp, _Alloc>& std::vector<_Tp, _Alloc>::operator=(const std::vector<_Tp, _Alloc>&) [with _Tp = greedy_ops::X; _Alloc = std::allocator<greedy_ops::X>]’ at /home/jwakely/gcc/14/include/c++/14.0.0/bits/vector.tcc:255:17, inlined from ‘int main()’ at /home/jwakely/src/gcc/gcc/libstdc++-v3/testsuite/23_containers/vector/types/1.cc:35:7: /home/jwakely/gcc/14/include/c++/14.0.0/bits/stl_algobase.h:437:30: warning: ‘void* __builtin_memcpy(void*, const void*, long unsigned int)’ forming offset 1 is out of the bounds [0, 1] [-Warray-bounds=] 437 | __builtin_memmove(__result, __first, sizeof(_Tp) * _Num); | ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In static member function ‘static _Up* std::__copy_move<_IsMove, true, std::random_access_iterator_tag>::__copy_m(_Tp*, _Tp*, _Up*) [with _Tp = greedy_ops::X; _Up = greedy_ops::X; bool _IsMove = false]’, inlined from ‘_OI std::__copy_move_a2(_II, _II, _OI) [with bool _IsMove = false; _II = greedy_ops::X*; _OI = greedy_ops::X*]’ at /home/jwakely/gcc/14/include/c++/14.0.0/bits/stl_algobase.h:506:30, inlined from ‘_OI std::__copy_move_a1(_II, _II, _OI) [with bool _IsMove = false; _II = greedy_ops::X*; _OI = greedy_ops::X*]’ at /home/jwakely/gcc/14/include/c++/14.0.0/bits/stl_algobase.h:533:42, inlined from ‘_OI std::__copy_move_a(_II, _II, _OI) [with bool _IsMove = false; _II = greedy_ops::X*; _OI = greedy_ops::X*]’ at /home/jwakely/gcc/14/include/c++/14.0.0/bits/stl_algobase.h:540:31, inlined from ‘_OI std::copy(_II, _II, _OI) [with _II = greedy_ops::X*; _OI = greedy_ops::X*]’ at /home/jwakely/gcc/14/include/c++/14.0.0/bits/stl_algobase.h:633:7, inlined from ‘static _ForwardIterator std::__uninitialized_copy<true>::__uninit_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = greedy_ops::X*; _ForwardIterator = greedy_ops::X*]’ at /home/jwakely/gcc/14/include/c++/14.0.0/bits/stl_uninitialized.h:147:27, inlined from ‘_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = greedy_ops::X*; _ForwardIterator = greedy_ops::X*]’ at /home/jwakely/gcc/14/include/c++/14.0.0/bits/stl_uninitialized.h:185:15, inlined from ‘_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, allocator<_Tp>&) [with _InputIterator = greedy_ops::X*; _ForwardIterator = greedy_ops::X*; _Tp = greedy_ops::X]’ at /home/jwakely/gcc/14/include/c++/14.0.0/bits/stl_uninitialized.h:373:37, inlined from ‘std::vector<_Tp, _Alloc>& std::vector<_Tp, _Alloc>::operator=(const std::vector<_Tp, _Alloc>&) [with _Tp = greedy_ops::X; _Alloc = std::allocator<greedy_ops::X>]’ at /home/jwakely/gcc/14/include/c++/14.0.0/bits/vector.tcc:257:35, inlined from ‘int main()’ at /home/jwakely/src/gcc/gcc/libstdc++-v3/testsuite/23_containers/vector/types/1.cc:35:7: /home/jwakely/gcc/14/include/c++/14.0.0/bits/stl_algobase.h:437:30: warning: ‘void* __builtin_memcpy(void*, const void*, long unsigned int)’ reading between 2 and 9223372036854775807 bytes from a region of size 1 [-Wstringop-overread] 437 | __builtin_memmove(__result, __first, sizeof(_Tp) * _Num); | ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In file included from /home/jwakely/gcc/14/include/c++/14.0.0/x86_64-pc-linux-gnu/bits/c++allocator.h:33, from /home/jwakely/gcc/14/include/c++/14.0.0/bits/allocator.h:46, from /home/jwakely/gcc/14/include/c++/14.0.0/vector:63: In member function ‘_Tp* std::__new_allocator<_Tp>::allocate(size_type, const void*) [with _Tp = greedy_ops::X]’, inlined from ‘static _Tp* std::allocator_traits<std::allocator<_Tp1> >::allocate(allocator_type&, size_type) [with _Tp = greedy_ops::X]’ at /home/jwakely/gcc/14/include/c++/14.0.0/bits/alloc_traits.h:482:28, inlined from ‘std::_Vector_base<_Tp, _Alloc>::pointer std::_Vector_base<_Tp, _Alloc>::_M_allocate(std::size_t) [with _Tp = greedy_ops::X; _Alloc = std::allocator<greedy_ops::X>]’ at /home/jwakely/gcc/14/include/c++/14.0.0/bits/stl_vector.h:378:33, inlined from ‘std::_Vector_base<_Tp, _Alloc>::pointer std::_Vector_base<_Tp, _Alloc>::_M_allocate(std::size_t) [with _Tp = greedy_ops::X; _Alloc = std::allocator<greedy_ops::X>]’ at /home/jwakely/gcc/14/include/c++/14.0.0/bits/stl_vector.h:375:7, inlined from ‘void std::_Vector_base<_Tp, _Alloc>::_M_create_storage(std::size_t) [with _Tp = greedy_ops::X; _Alloc = std::allocator<greedy_ops::X>]’ at /home/jwakely/gcc/14/include/c++/14.0.0/bits/stl_vector.h:413:44, inlined from ‘std::_Vector_base<_Tp, _Alloc>::_Vector_base(std::size_t, const allocator_type&) [with _Tp = greedy_ops::X; _Alloc = std::allocator<greedy_ops::X>]’ at /home/jwakely/gcc/14/include/c++/14.0.0/bits/stl_vector.h:332:26, inlined from ‘std::vector<_Tp, _Alloc>::vector(size_type, const allocator_type&) [with _Tp = greedy_ops::X; _Alloc = std::allocator<greedy_ops::X>]’ at /home/jwakely/gcc/14/include/c++/14.0.0/bits/stl_vector.h:572:47, inlined from ‘int main()’ at /home/jwakely/src/gcc/gcc/libstdc++-v3/testsuite/23_containers/vector/types/1.cc:29:39: /home/jwakely/gcc/14/include/c++/14.0.0/bits/new_allocator.h:147:48: note: source object of size 1 allocated by ‘operator new’ 147 | return static_cast<_Tp*>(_GLIBCXX_OPERATOR_NEW(__n * sizeof(_Tp))); | ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~ The preprocessed source of the test is attached (with the proposed new hints added to the std::vector code). There's no warning with -O1 or -O3, only -O2. Referenced Bugs: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97048 [Bug 97048] [meta-bug] bogus/missing -Wstringop-overread warnings