https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99632
Bug ID: 99632
Summary: missing warning accessing a trailing zero length array
member of base class
Product: gcc
Version: 11.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: tree-optimization
Assignee: unassigned at gcc dot gnu.org
Reporter: msebor at gcc dot gnu.org
Target Milestone: ---
-Warray-bounds triggers for the invalid access in f() and g() but not in h().
All three should be diagnosed. In addition, the warning in g() should be on
the line with the access.
$ cat t.C && gcc -O2 -S -Wall t.C
struct D: B { int j; };
void f (D *p, int i)
{
p->a[i] = 0; // -Warray-bounds (good)
}
void g (D *p, int i)
{
int *q = &p->a[i]; //-Warray-bounds (should be on next line)
*q = 0;
}
void h (D *p, int i)
{
int *q = p->a;
q[i] = 0; // missing warning
}
t.C: In function ‘void f(D*, int)’:
t.C:6:9: warning: array subscript i is outside array bounds of ‘int [0]’
[-Warray-bounds]
6 | p->a[i] = 0; // -Warray-bounds (good)
| ~~~~~~^
t.C:1:19: note: while referencing ‘B::a’
1 | struct B { int n, a[0]; };
| ^
t.C: In function ‘void g(D*, int)’:
t.C:11:19: warning: array subscript i is outside array bounds of ‘int [0]’
[-Warray-bounds]
11 | int *q = &p->a[i]; //-Warray-bounds (should be on next line)
| ~~~~~~^
t.C:1:19: note: while referencing ‘B::a’
1 | struct B { int n, a[0]; };
| ^