This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug rtl-optimization/52186] New: array out of bounds error when accessing last byte of a struct via char ptr
- From: "alex at aivor dot com" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Thu, 09 Feb 2012 17:09:52 +0000
- Subject: [Bug rtl-optimization/52186] New: array out of bounds error when accessing last byte of a struct via char ptr
- Auto-submitted: auto-generated
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52186
Bug #: 52186
Summary: array out of bounds error when accessing last byte of
a struct via char ptr
Classification: Unclassified
Product: gcc
Version: 4.5.2
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: rtl-optimization
AssignedTo: unassigned@gcc.gnu.org
ReportedBy: alex@aivor.com
Created attachment 26624
--> http://gcc.gnu.org/bugzilla/attachment.cgi?id=26624
source code
In some cases, casting a struct to an unsigned char and indexing the last byte
causes an array out of bounds error. To illustrate:
struct foobar foobar;
unsigned char *k = (unsigned char*)foobar;
if ( k[sizeof(struct foobar)-1] ) /* out of bounds? */
return;
The above snippet is only to describe the problem. See the attached file
"test.c" for an real example. When test.c is compiled with -O2 it gives the
following error:
test.c:21:11: error: array subscript is above array bounds
Small changes to the code such as changing a struct member type from 'unsigned
char' to 'char' avoid the error. Other changes that avoid the error are
described in comments in test.c.
The error is flagged with and without '-fno-strict-aliasing'.
The file test.c is as simple as I could make it -- no external includes, etc.
Attached files:
test.c -- source code
test.* -- files created by '-save-temps'
log -- stdout and stderr of the compile command
Compile command:
gcc -v -save-temps -c test.c -Wall -Werror -O2 -fno-strict-aliasing