Prior to version 4.7, GCC would diagnose out-of-bounds accesses to local arrays across inlined function boundaries. The test case below shows that these invalid accesses are no longer diagnosed. int f (int i) { int a[] = { 1, 2 }; if (i < 3) i = 3; return a[i]; // -Warray-bounds (good) } int g (const int *p, int i) { return p[i]; } int h (int i) { int a[] = { 2, 3 }; if (i < 3) i = 3; return g (a, i); // missing -Warray-bounds } t.c: In function ‘f’: t.c:8:11: warning: array subscript 3 is above array bounds of ‘int[2]’ [-Warray-bounds] return a[i]; // -Warray-bounds (good) ~^~~
Bisection points to r178312 committed in GCC 4.7 as the change that introduced this regression: r178312 | rguenth | 2011-08-30 10:06:00 -0400 (Tue, 30 Aug 2011) | 30 lines 2011-08-30 Richard Guenther <rguenther@suse.de> PR middle-end/48571
Confirmed.
Fixed in GCC 9. > ./cc1 -quiet t.c -O2 -Warray-bounds t.c: In function ‘h’: t.c:3:13: warning: array subscript [3, 2147483647] is outside array bounds of ‘int[2]’ [-Warray-bounds] 3 | return p[i]; | ~^~~ t.c:8:9: note: while referencing ‘a’ 8 | int a[] = { 2, 3 }; | ^
It was fixed in r262893 but none of the tests verifies that it works. Let me add one.
Actually, the new test revealed that it doesn't work in ILP32 even in GCC 9: $ gcc -O2 -S -Wall -m32 pr84053.c pr84053.c: In function ‘f’: pr84053.c:9:11: warning: array subscript 3 is above array bounds of ‘int[2]’ [-Warray-bounds] 9 | return a[i]; // -Warray-bounds (good) | ~^~~ pr84053.c:4:7: note: while referencing ‘a’ 4 | int a[] = { 1, 2 }; | ^ Let me add the test anyway and xfail it for ILP32.
Author: msebor Date: Thu Dec 20 16:25:13 2018 New Revision: 267302 URL: https://gcc.gnu.org/viewcvs?rev=267302&root=gcc&view=rev Log: PR tree-optimization/84053 - missing -Warray-bounds accessing a local array across inlined function boundaries gcc/testsuite/ChangeLog: * gcc.dg/Warray-bounds-36.c: New test. Added: trunk/gcc/testsuite/gcc.dg/Warray-bounds-36.c Modified: trunk/gcc/testsuite/ChangeLog
Let me also restore the regression marker for 9 since the regression is still present for ILP32.
GCC 9.1 has been released.
GCC 9.2 has been released.
GCC 9.3.0 has been released, adjusting target milestone.
GCC 9.4 is being released, retargeting bugs to GCC 9.5.
Fixed for GCC 10.