https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96346
Bug ID: 96346 Summary: missing warning accessing an element of a non-trailing zero length array Product: gcc Version: 10.1.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: middle-end Assignee: unassigned at gcc dot gnu.org Reporter: msebor at gcc dot gnu.org Target Milestone: --- GCC treats trailing arrays of zero length as flexible array members provided they are a) single-dimensional and b) not nested in another structure. This is reflected in the comments in array_at_struct_end_p(): /* If the reference chain contains a component reference to a non-union type and there follows another field the reference is not at the end of a structure. */ if (TREE_CODE (ref) == COMPONENT_REF) { ... } /* If we have a multi-dimensional array we do not consider a non-innermost dimension as flex array if the whole multi-dimensional array is at struct end. Same for an array of aggregates with a trailing array member. */ To help prevent bugs in code that assumes otherwise -Warray-bounds should diagnose attempts to access elements of zero-length arrays that aren't treated as "flexible array members," such as those in the test case below: struct A { char n, a[0]; }; struct B { struct A a; }; int f (struct B *p) { return p->a.a[0]; // missing warning } struct C { char a[0][1]; }; int g (struct C *p) { return p->a[0][0]; // missing warning }