https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92326
Bug ID: 92326
Summary: wrong bound in zero-length array diagnostics
Product: gcc
Version: 10.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: msebor at gcc dot gnu.org
Target Milestone: ---
r277728 changed the formatting of zero-length arrays in diagnostics to include
their bound, but the change also inadvertently added the zero bound to flexible
array members (because they are gratuitously represented differently between
the front-ends). The test case below shows the problem. It only affects C
code:
$ cat x.c && gcc -O2 -S -Wall x.c
struct S0 { int n, a[0]; } s0;
struct Sx { int n, a[]; } sx = { 0 };
void f (void)
{
s0.a[0] = 0;
sx.a[0] = 0;
}
x.c: In function ‘f’:
x.c:6:7: warning: array subscript 0 is above array bounds of ‘int[0]’
[-Warray-bounds]
6 | s0.a[0] = 0;
| ~~~~^~~
x.c:1:20: note: while referencing ‘a’
1 | struct S0 { int n, a[0]; } s0;
| ^
x.c:1:28: note: defined here ‘s0’
1 | struct S0 { int n, a[0]; } s0;
| ^~
x.c:7:7: warning: array subscript 0 is above array bounds of ‘int[0]’
[-Warray-bounds]
7 | sx.a[0] = 0;
| ~~~~^~~
x.c:2:20: note: while referencing ‘a’
2 | struct Sx { int n, a[]; } sx = { 0 };
| ^
x.c:2:27: note: defined here ‘sx’
2 | struct Sx { int n, a[]; } sx = { 0 };
| ^~
When compiled by the C++ front-end, the format of the arrays is as expected:
$ gcc -O2 -S -Wall -xc++ x.c
x.c: In function ‘void f()’:
x.c:6:9: warning: array subscript 0 is above array bounds of ‘int [0]’
[-Warray-bounds]
6 | s0.a[0] = 0;
| ~~~~~~^
x.c:1:20: note: while referencing ‘S0::a’
1 | struct S0 { int n, a[0]; } s0;
| ^
x.c:1:28: note: defined here ‘s0’
1 | struct S0 { int n, a[0]; } s0;
| ^~
x.c:7:9: warning: array subscript 0 is above array bounds of ‘int []’
[-Warray-bounds]
7 | sx.a[0] = 0;
| ~~~~~~^
x.c:2:20: note: while referencing ‘Sx::a’
2 | struct Sx { int n, a[]; } sx = { 0 };
| ^
x.c:2:27: note: defined here ‘sx’
2 | struct Sx { int n, a[]; } sx = { 0 };
| ^~