https://gcc.gnu.org/bugzilla/show_bug.cgi?id=117829
Bug ID: 117829
Summary: False positive report for Warray-bounds with -O2
Product: gcc
Version: 12.1.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: tree-optimization
Assignee: unassigned at gcc dot gnu.org
Reporter: tobias.trauth at x2e dot de
Target Milestone: ---
Hello everyone,
I encountered a bug for g++-12 and newer.
Its seems that g++ reports a false positive for Warray-bounds if at least O2 is
avtive.
```
#include <vector>
#include <algorithm>
int main()
{
// Block #1
std::vector<int> c2 = {};
auto it = std::find(c2.begin(), c2.end(), 2);
if(it != c2.end())
{
c2.erase(it);
}
// End of block #1
// Block #2
std::vector<int> c = {};
c.erase(std::remove(c.begin(), c.end(), 2), c.end());
// End of block #2
return 0;
}
```
g++ arguments: --std=c++17 -Wall -Wextra -O2
Output:
```
In file included from
/opt/compiler-explorer/gcc-12.1.0/include/c++/12.1.0/x86_64-linux-gnu/bits/c++allocator.h:33,
from
/opt/compiler-explorer/gcc-12.1.0/include/c++/12.1.0/bits/allocator.h:46,
from
/opt/compiler-explorer/gcc-12.1.0/include/c++/12.1.0/vector:61,
from <source>:1:
In member function 'void std::__new_allocator<_Tp>::destroy(_Up*) [with _Up =
int; _Tp = int]',
inlined from 'static void std::allocator_traits<std::allocator<_Tp1>
>::destroy(allocator_type&, _Up*) [with _Up = int; _Tp = int]' at
/opt/compiler-explorer/gcc-12.1.0/include/c++/12.1.0/bits/alloc_traits.h:535:15,
inlined from 'std::vector<_Tp, _Alloc>::iterator std::vector<_Tp,
_Alloc>::_M_erase(iterator) [with _Tp = int; _Alloc = std::allocator<int>]' at
/opt/compiler-explorer/gcc-12.1.0/include/c++/12.1.0/bits/vector.tcc:181:29,
inlined from 'std::vector<_Tp, _Alloc>::iterator std::vector<_Tp,
_Alloc>::erase(const_iterator) [with _Tp = int; _Alloc = std::allocator<int>]'
at
/opt/compiler-explorer/gcc-12.1.0/include/c++/12.1.0/bits/stl_vector.h:1530:24,
inlined from 'int main()' at <source>:11:11:
/opt/compiler-explorer/gcc-12.1.0/include/c++/12.1.0/bits/new_allocator.h:181:11:
warning: array subscript -1 is outside array bounds of 'int
[2305843009213693951]' [-Warray-bounds]
181 | { __p->~_Up(); }
| ^~~
ASM generation compiler returned: 0
```
This happens only if:
1.) At least compile with O2
2.) std=c++17 is used
3.) Both "blocks" are enabled. If one is commented out, it works.
Here is a direct link to the above sample: https://godbolt.org/z/cxbfYTfP1
BR