https://gcc.gnu.org/bugzilla/show_bug.cgi?id=119797
Bug ID: 119797
Summary: Incorrect Warning about Array Subscript out of Range
Product: gcc
Version: 14.2.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: christoph at muppetnet dot net
Target Milestone: ---
Target: ARM
When compiling the following code for ARM with -Wall -Wextra -O2
-mcpu=cortex-m4 we get an unexpected warning.
#include <cstdint>
template<uint8_t N>
uint32_t Test(uint8_t r)
{
if (r >= 16)
{
return 2U;
}
else
{
return N;
}
}
const uint32_t BUG[] = { 0x0000000U, 0x0000001U, 0x0000002U };
bool Bug(uint8_t r)
{
const uint32_t s = Test<0U>(r);
const uint32_t e = Test<1U>(r);
const uint32_t cs = BUG[r];
const uint32_t ce = BUG[r];
if ((s != cs) || (e != ce))
{
return false;
}
else
{
return true;
}
}
The compiler complains about r being our of range. The "16" here is related to
the "if (r >= 16)". Changing the if results also in a different warning shown.
<source>: In function 'bool Bug(uint8_t)':
<source>:23:30: warning: array subscript 16 is above array bounds of 'const
uint32_t [3]' {aka 'const unsigned int [3]'} [-Warray-bounds=]
23 | const uint32_t cs = BUG[r];
| ~~~~~^
<source>:16:16: note: while referencing 'BUG'
16 | const uint32_t BUG[] = { 0x0000000U, 0x0000001U, 0x0000002U };
| ^~~
Compiler returned: 0
The issue can be seen here https://godbolt.org/z/j6WozvMb3