[PATCH] enhance -Warray-bounds to detect out-of-bounds offsets (PR 82455)
Martin Sebor
msebor@gmail.com
Sun Oct 29 16:15:00 GMT 2017
In my work on -Wrestrict, to issue meaningful warnings, I found
it important to detect both out of bounds array indices as well
as offsets in calls to restrict-qualified functions like strcpy.
GCC already detects some of these cases but my tests for
the enhanced warning exposed a few gaps.
The attached patch enhances -Warray-bounds to detect more instances
out-of-bounds indices and offsets to member arrays and non-array
members. For example, it detects the out-of-bounds offset in the
call to strcpy below.
The patch is meant to be applied on top posted here but not yet
committed:
https://gcc.gnu.org/ml/gcc-patches/2017-10/msg01304.html
Richard, since this also touches tree-vrp.c I look for your comments.
Jeff, this is the enhancement you were interested in when we spoke
last week.
Thanks
Martin
$ cat a.c && gcc -O2 -S -Wall a.c
struct A { char a[4]; void (*pf)(void); };
void f (struct A *p)
{
p->a[5] = 'x'; // existing -Warray-bounds
strcpy (p->a + 6, "y"); // enhanced -Warray-bounds
}
a.c: In function âfâ:
a.c:7:3: warning: offset 6 is out of bounds of âchar[4]â [-Warray-bounds]
strcpy (p->a + 6, "y");
^~~~~~~~~~~~~~~~~~~~~~
a.c:1:17: note: member declared here
struct A { char a[4]; void (*pf)(void); };
^
a.c:5:7: warning: array subscript 5 is above array bounds of
âchar[4]â [-Warray-bounds]
p->a[5] = 'x';
~~~~^~~
-------------- next part --------------
A non-text attachment was scrubbed...
Name: gcc-82455.diff
Type: text/x-patch
Size: 28853 bytes
Desc: not available
URL: <http://gcc.gnu.org/pipermail/gcc-patches/attachments/20171029/d1d61336/attachment.bin>
More information about the Gcc-patches
mailing list