[Bug c++/105438] New: Incorrect array-bounds warning with array size carried over from a previous template instantiation
bernie at codewiz dot org
gcc-bugzilla@gcc.gnu.org
Sat Apr 30 08:24:55 GMT 2022
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105438
Bug ID: 105438
Summary: Incorrect array-bounds warning with array size carried
over from a previous template instantiation
Product: gcc
Version: 11.3.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: bernie at codewiz dot org
Target Milestone: ---
Minified testcase (almost every line is necessary to reproduce):
```
/* g++ -Warray-bounds -O2 repro.cc */
int longer[7] = {};
int shorter[2] = {};
int out[10] = {};
template <int N>
void configure(const int(&in)[N], const int nrows = N)
{
if (nrows <= 10)
{
for (int i = 0; i < nrows; i++)
{
out[i] = in[i];
}
}
}
int main()
{
configure(longer);
configure(shorter);
}
```
Output:
```
$ g++ -Warray-bounds -O2 repro.cc
repro.cc: In function 'int main()':
repro.cc:13:24: warning: array subscript 'const int [7][0]' is partly outside
array bounds of 'int [2]' [-Warray-bounds]
13 | out[i] = in[i];
| ~~^
repro.cc:3:5: note: while referencing 'shorter'
3 | int shorter[2] = {};
| ^~~~~~~
repro.cc:13:24: warning: array subscript 'const int [7][0]' is partly outside
array bounds of 'int [2]' [-Warray-bounds]
13 | out[i] = in[i];
| ~~^
repro.cc:3:5: note: while referencing 'shorter'
3 | int shorter[2] = {};
| ^~~~~~~
```
Static analysis appears to be using the length of the longer array for the call
using the shorter array.
The warning disappears by:
* commenting out the first call to configure() suppresses the warning
* swapping the two calls to configure()
* commenting out if statement also eliminates the warning
* making longer and shorter the same size
* using N as loop counter instead of nrows
More information about the Gcc-bugs
mailing list