[Bug c++/106901] New: False positive -Warray-bounds with -O2 or higher?
carlosgalvezp at gmail dot com
gcc-bugzilla@gcc.gnu.org
Sat Sep 10 17:06:53 GMT 2022
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106901
Bug ID: 106901
Summary: False positive -Warray-bounds with -O2 or higher?
Product: gcc
Version: 13.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: carlosgalvezp at gmail dot com
Target Milestone: ---
Hi!
Here's some code snippet that I believe leads to a false positive for
Warray-bounds. It happens in trunk but not on GCC 12.2.
#include <array>
bool bar(std::array<int, 5> const& vec,
std::size_t const size,
std::size_t const expected_size)
{
if (size < expected_size)
{
return false;
}
for (std::size_t i{expected_size}; i < size; ++i)
{
if (vec[i] != 0)
{
return false;
}
}
return true;
}
bool foo(std::array<int, 5> const& vec, std::size_t const size)
{
return bar(vec, size, 5);
}
In file included from <source>:1:
In member function 'constexpr const std::array<_Tp, _Nm>::value_type&
std::array<_Tp, _Nm>::operator[](size_type) const [with _Tp = int; long
unsigned int _Nm = 5]',
inlined from 'bool bar(const std::array<int, 5>&, std::size_t,
std::size_t)' at <source>:13:18,
inlined from 'bool foo(const std::array<int, 5>&, std::size_t)' at
<source>:23:15:
/opt/compiler-explorer/gcc-trunk-20220910/include/c++/13.0.0/array:213:24:
warning: array subscript 5 is above array bounds of 'std::__array_traits<int,
5>::_Type' {aka 'const int [5]'} [-Warray-bounds]
213 | return _M_elems[__n];
| ~~~~~~~~^
/opt/compiler-explorer/gcc-trunk-20220910/include/c++/13.0.0/array: In function
'bool foo(const std::array<int, 5>&, std::size_t)':
/opt/compiler-explorer/gcc-trunk-20220910/include/c++/13.0.0/array:109:55:
note: while referencing 'std::array<int, 5>::_M_elems'
109 | typename __array_traits<_Tp, _Nm>::_Type _M_elems;
| ^~~~~~~~
Example on Compiler Explorer:
https://godbolt.org/z/dKWdKrsTa
Since "size" is unknown to the compiler, it shouldn't be possible to tell with
certainty whether the loop is actually executed and therefore out-of-bounds
invoked.
Thanks!
More information about the Gcc-bugs
mailing list