This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c/71411] New: No warnings for OOB accesses with arrays of size 1
- From: "ch3root at openwall dot com" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Sat, 04 Jun 2016 12:22:35 +0000
- Subject: [Bug c/71411] New: No warnings for OOB accesses with arrays of size 1
- Auto-submitted: auto-generated
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71411
Bug ID: 71411
Summary: No warnings for OOB accesses with arrays of size 1
Product: gcc
Version: 7.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: ch3root at openwall dot com
Target Milestone: ---
In this example rows in a 2D array are arrays of size 1. gcc is smart enough to
conclude that an unknown index have to be 0 but doesn't warn about known index
1. Raising the level of -Warray-bounds to 2 doesn't help.
Source code:
----------------------------------------------------------------------
#include <stdio.h>
int main(int c, char **v)
{
(void)v;
char a[2][1] = {{5}, {7}};
printf("c = %d\n", c);
printf("a[0][c] = %d\n", a[0][c]);
printf("a[0][1] = %d\n", a[0][1]);
}
----------------------------------------------------------------------
Results:
----------------------------------------------------------------------
$ gcc -std=c11 -pedantic -Wall -Wextra -Warray-bounds=2 -O3 test.c && ./a.out
c = 1
a[0][c] = 5
a[0][1] = 7
----------------------------------------------------------------------
gcc version: gcc (GCC) 7.0.0 20160604 (experimental)
For comparison:
----------------------------------------------------------------------
$ clang -std=c11 -Weverything -O3 test.c && ./a.out
test.c:11:28: warning: array index 1 is past the end of the array (which
contains 1 element) [-Warray-bounds]
printf("a[0][1] = %d\n", a[0][1]);
^ ~
test.c:7:3: note: array 'a' declared here
char a[2][1] = {{5}, {7}};
^
1 warning generated.
c = 1
a[0][c] = 7
a[0][1] = 7
----------------------------------------------------------------------
clang version: clang version 3.9.0 (trunk 271312)