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

            Bug ID: 128679
           Summary: [16 Regression] bogus -Warray-bounds in
                    std::vector<char>::resize(1) at -O3
           Product: gcc
           Version: 16.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: liweifriends at gmail dot com
  Target Milestone: ---

```cpp
// g++ -O3 -Wall -c pr.cpp
#include <vector>
void f(std::vector<char>& v) { v.resize(1); }
```

With g++ 16.2.1 20261029 (Red Hat 16.2.1-3):

```
In function '... std::__fill_a1(_Up*, _Up*, const _Tp&) [with _Up = char; _Tp =
char]',
    ...
    inlined from 'void std::vector<_Tp, _Alloc>::_M_default_append(size_type)
[with _Tp = char; ...]' at /usr/include/c++/16/bits/vector.tcc:862:35,
    inlined from 'void std::vector<_Tp, _Alloc>::resize(size_type) [with _Tp =
char; ...]' at /usr/include/c++/16/bits/stl_vector.h:1136:21,
    inlined from 'void f(std::vector<char>&)' at pr.cpp:3:40:
/usr/include/c++/16/bits/stl_algobase.h:949:25: warning: 'void*
__builtin_memset(void*, int, long unsigned int)' offset 1 is out of the bounds
[0, 1] [-Warray-bounds=]
```

vector.tcc:862 is the reallocation branch of `_M_default_append`, which fills
`__n` elements at `__new_start + __size` in a new buffer of `__len` elements.
The warning needs `__len == 1` with `__size == 1`, but `_M_check_len`
guarantees `__len >= __size + __n`.

It warns at -O3 and is silent at -O2. The cause is the larger early-inlining
budget, not an -O3 pass: `-O2 --param early-inlining-insns=8` already warns
(the -O3 default is 14), and `-O3 --param early-inlining-insns=6` does not.
Each -O3-only flag was also toggled on its own, and none of them changes the
result. It also needs a byte-sized element type (char, signed/unsigned char,
char8_t, std::byte; not bool, short or int) and the constant 1 (not 2, not a
run-time n). Every -std from c++11 to c++26 warns.

Regression: g++-15 15.3.1 20260722 (Red Hat 15.3.1-1), on the same machine, is
silent at -O2, at -O3, and at -O2 --param early-inlining-insns=14. It also
compiles the original project file at -O3 -Werror.

Not a duplicate of PR 114945 or PR 123850. Both are the in-place branch, and
the testcase from 123850 is silent with 16.2.1. Not a duplicate of PR 122197
either: there is no virtual call, and -fno-devirtualize-speculatively does not
help.

Reply via email to