https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67872
Bug ID: 67872
Summary: missing -Warray-bounds warning, bogus
-Wmaybe-uninitialized
Product: gcc
Version: 6.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: ---
GCC doesn't diagnose either of the out of bounds accesses in the following
program unless -O2 is used. At -O1 and above, GCC issues a bogus
-Wmaybe-uninitialized warning about the access to b[4] but no -Warray-bounds
warning:
$ cat z.c && gcc -c z.c -Wall
int a[] = { 1, 2, 3 };
int foo (int i)
{
int b[] = { 4, 5, 6};
if (i)
return a [4];
else
return b [4];
}
$
Clang, on the other hand, diagnoses both accesses with the expected warning
regardless of optimization:
clang -Wall -S -c -o/dev/null z.c
z.c:7:16: warning: array index 4 is past the end of the array (which contains 3
elements) [-Warray-bounds]
return a [4];
^ ~
z.c:1:1: note: array 'a' declared here
int a[] = { 1, 2, 3 };
^
z.c:9:16: warning: array index 4 is past the end of the array (which contains 3
elements) [-Warray-bounds]
return b [4];
^ ~
z.c:5:5: note: array 'b' declared here
int b[] = { 4, 5, 6};
^
2 warnings generated.