https://gcc.gnu.org/bugzilla/show_bug.cgi?id=123850

            Bug ID: 123850
           Summary: vector resize: bits/stl_construct.h:133:7: error:
                    array subscript 5 is outside array bounds of ‘char
                    [5]’ [-Werror=array-bounds=]
           Product: gcc
           Version: 15.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: syq at gcc dot gnu.org
  Target Milestone: ---

```
#include <vector>
int main() {
        std::vector<char> v = {1, 2, 3, 4, 5};
        v.resize(49);
}
```

`g++ -std=c++20 -O2 -Wall -Werror -S xx.cc`:
```
../xx.cc:1:10: fatal error: vector: No such file or directory
    1 | #include <vector>
      |          ^~~~~~~~
compilation terminated.
syq@vmjh:/data/syq/git-push/gcc/build-master$ ./gcc/xg++ -Bgcc -I
x86_64-pc-linux-gnu/libstdc++-v3/include -I
./x86_64-pc-linux-gnu/libstdc++-v3/include/x86_64-pc-linux-gnu
-I.././libstdc++-v3/libsupc++/ -O2 -Werror -Wall -S ../xx.cc
In file included from x86_64-pc-linux-gnu/libstdc++-v3/include/vector:66,
                 from ../xx.cc:1:
In function ‘void std::_Construct(_Tp*, _Args&& ...) [with _Tp = char; _Args =
{}]’,
    inlined from ‘static _ForwardIterator
std::__uninitialized_default_n_1<true>::__uninit_default_n(_ForwardIterator,
_Size) [with _ForwardIterator = char*; _Size = long unsigned int]’ at
x86_64-pc-linux-gnu/libstdc++-v3/include/bits/stl_uninitialized.h:894:23,
    inlined from ‘static _ForwardIterator
std::__uninitialized_default_n_1<true>::__uninit_default_n(_ForwardIterator,
_Size) [with _ForwardIterator = char*; _Size = long unsigned int]’ at
x86_64-pc-linux-gnu/libstdc++-v3/include/bits/stl_uninitialized.h:888:9,
    inlined from ‘_ForwardIterator
std::__uninitialized_default_n(_ForwardIterator, _Size) [with _ForwardIterator
= char*; _Size = long unsigned int]’ at
x86_64-pc-linux-gnu/libstdc++-v3/include/bits/stl_uninitialized.h:941:20,
    inlined from ‘_ForwardIterator
std::__uninitialized_default_n_a(_ForwardIterator, _Size, allocator<_Tp2>&)
[with _ForwardIterator = char*; _Size = long unsigned int; _Tp = char]’ at
x86_64-pc-linux-gnu/libstdc++-v3/include/bits/stl_uninitialized.h:996:44,
    inlined from ‘void std::vector<_Tp, _Alloc>::_M_default_append(size_type)
[with _Tp = char; _Alloc = std::allocator<char>]’ at
x86_64-pc-linux-gnu/libstdc++-v3/include/bits/vector.tcc:773:35,
    inlined from ‘void std::vector<_Tp, _Alloc>::resize(size_type) [with _Tp =
char; _Alloc = std::allocator<char>]’ at
x86_64-pc-linux-gnu/libstdc++-v3/include/bits/stl_vector.h:1146:21,
    inlined from ‘int main()’ at ../xx.cc:4:17:
x86_64-pc-linux-gnu/libstdc++-v3/include/bits/stl_construct.h:133:7: error:
array subscript 5 is outside array bounds of ‘char [5]’ [-Werror=array-bounds=]
  133 |       ::new(static_cast<void*>(__p))
_Tp(std::forward<_Args>(__args)...);
      |      
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from
./x86_64-pc-linux-gnu/libstdc++-v3/include/x86_64-pc-linux-gnu/bits/c++allocator.h:33,
                 from
x86_64-pc-linux-gnu/libstdc++-v3/include/bits/allocator.h:46,
                 from x86_64-pc-linux-gnu/libstdc++-v3/include/vector:65:
In member function ‘_Tp* std::__new_allocator<_Tp>::allocate(size_type, const
void*) [with _Tp = char]’,
    inlined from ‘static _Tp* std::allocator_traits<std::allocator<_Tp1>
>::allocate(allocator_type&, size_type) [with _Tp = char]’ at
x86_64-pc-linux-gnu/libstdc++-v3/include/bits/alloc_traits.h:614:28,
    inlined from ‘std::_Vector_base<_Tp, _Alloc>::pointer
std::_Vector_base<_Tp, _Alloc>::_M_allocate(std::size_t) [with _Tp = char;
_Alloc = std::allocator<char>]’ at
x86_64-pc-linux-gnu/libstdc++-v3/include/bits/stl_vector.h:387:33,
    inlined from ‘void std::vector<_Tp,
_Alloc>::_M_range_initialize_n(_Iterator, _Sentinel, size_type) [with _Iterator
= const char*; _Sentinel = const char*; _Tp = char; _Alloc =
std::allocator<char>]’ at
x86_64-pc-linux-gnu/libstdc++-v3/include/bits/stl_vector.h:1985:23,
    inlined from ‘std::vector<_Tp, _Alloc>::vector(std::initializer_list<_Tp>,
const allocator_type&) [with _Tp = char; _Alloc = std::allocator<char>]’ at
x86_64-pc-linux-gnu/libstdc++-v3/include/bits/stl_vector.h:712:23,
    inlined from ‘int main()’ at ../xx.cc:3:45:
x86_64-pc-linux-gnu/libstdc++-v3/include/bits/new_allocator.h:151:73: note: at
offset 5 into object of size 5 allocated by ‘operator new’
  151 |         return static_cast<_Tp*>(_GLIBCXX_OPERATOR_NEW(__n *
sizeof(_Tp)));
      |                                                                        
^
cc1plus: all warnings being treated as errors
```


This problem first is from
```
commit e200f53a5556516ec831e6b7a34aaa0f10a4ab0a (HEAD)
Author: Jonathan Wakely <[email protected]>
Date:   Tue Mar 25 13:24:08 2025 +0000

    libstdc++: Optimize std::vector construction from input iterators
[PR108487]
```

and disappears after
```
commit 064cac730f88dc71c6da578f9ae5b8e092ab6cd4
Author: Jan Hubicka <[email protected]>
Date:   Sun May 4 10:52:35 2025 +0200

    Improve maybe_hot handling in inliner heuristics
```

Only gcc-15 is affected.

Reply via email to