[Bug c++/124986] New: [12/13/14/15 Regression] -Warray-bounds and -Wstringop-overread false positives

andrea.agostini.sax at gmail dot com gcc-bugzilla@gcc.gnu.org
Wed Apr 22 12:16:35 GMT 2026


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

            Bug ID: 124986
           Summary: [12/13/14/15 Regression] -Warray-bounds and
                    -Wstringop-overread false positives
           Product: gcc
           Version: 15.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: andrea.agostini.sax at gmail dot com
  Target Milestone: ---

Created attachment 64273
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=64273&action=edit
preprocessed file

Compiling the following code emits a -Warray-bounds warning.
```
#include <vector>

bool included();

int main() {
    std::vector<int> vec = {1, 2, 3};

    if (included()) {
        vec.erase(vec.begin());
    }

    if (included()) {
//#pragma GCC diagnostic push
//#pragma GCC diagnostic ignored "-Warray-bounds"
//#pragma GCC diagnostic ignored "-Wstringop-overread"
        vec.erase(vec.begin(), vec.begin() + 2);
//#pragma GCC diagnostic pop
    }
}
```
Godbolt: https://godbolt.org/z/qd8TvYxWe

The attached preprocessed file was compiled with gcc 13.3.0 and -Wall -Werror
-O1 -save-temps.

Playing around in Compiler Explorer shows, the warning is emitted by gcc
12/13/14/15/trunk, but was not there in gcc 11.

When suppressing -Warray-bounds, a -Wstringop-overread pops up instead.

The warning is emitted with -O1 or higher, but not with -O0. The warning also
disappears if -fno-inline is used.

The warning is only emitted when, in the second call to erase, the second
iterator points to either end or one before end. I.e. replacing `vec.begin() +
2` by `vec.begin() + 3` in the above still triggers the warning. But the
warning is not triggered when replacing that by `vec.end()` instead.


More information about the Gcc-bugs mailing list