https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88565
Bug ID: 88565 Summary: enhance -Warray-bounds for C++ trailing class member arrays Product: gcc Version: 9.0 Status: UNCONFIRMED Severity: enhancement Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: msebor at gcc dot gnu.org Target Milestone: --- While thinking about pr84051 and about the test case in attachment 20033 to pr43270, it occurred to me that: GCC doesn't differentiate between out-of-bounds accesses to trailing member arrays in C or C++: in both languages it refrains from issuing -Warray-bounds=1 regardless of the array's bound. The rationale in C is that such arrays could be treated as flexible array members by the client code. But in the case of C++ classes this same logic is much less likely to apply because such uses are much less common there. It almost certainly doesn't apply when the array is a member of a class with a user-defined ctor. Objects of such classes are typically only initialized using the ctor which largely rules out allocating more space for the object than the size of its type implies. With this in mind, it would be appropriate to issue -Warray-bounds even at level 1 for the following code: struct S { S (); int f (); int a[3]; }; int S::f () { return a[7]; // request: issue -Warray-bounds=1 } This line of reasoning would also suggest that accesses to such arrays could be diagnosed even for zero-length arrays as requested in pr43270. Almost certainly when such an array is not the last member of a class as in the test case provided in that bug.