public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/63233] New: Valid out of bounds access leads to undefined behavior
@ 2014-09-11 21:15 leis at in dot tum.de
  2014-09-11 21:29 ` [Bug c/63233] " pinskia at gcc dot gnu.org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: leis at in dot tum.de @ 2014-09-11 21:15 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63233

            Bug ID: 63233
           Summary: Valid out of bounds access leads to undefined behavior
           Product: gcc
           Version: 4.9.1
            Status: UNCONFIRMED
          Severity: major
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: leis at in dot tum.de

Consider the following (minimized) program:

#include <stdlib.h>
#include <stdio.h>

struct Foo {
   int a[1];
   int b;
};

int main(int argc, char** argv) {
   int index = atoi(argv[1]);

   struct Foo foo;
   foo.a[1] = 99;

   printf("%d\n", foo.a[index]);

   return 0;
}

When compiled with -O1 or higher and called with 1 as command line argument an
undefined value instead of 99 is printed. In my understanding of the standard,
foo.a is a pointer and foo.a[1] is simply (foo.a+1), which is a perfectly fine
memory address.

This program always prints the expected value (99) with clang (3.5) and icc
(14) on all optimization levels. In gcc 4.3.4 I get the expected result on -O0
and -O1 but not on -O2 or -O3.


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2014-09-13  9:36 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-09-11 21:15 [Bug c/63233] New: Valid out of bounds access leads to undefined behavior leis at in dot tum.de
2014-09-11 21:29 ` [Bug c/63233] " pinskia at gcc dot gnu.org
2014-09-11 22:02 ` [Bug c/63233] Missing Warray-bounds warning for array within struct manu at gcc dot gnu.org
2014-09-11 22:14 ` pinskia at gcc dot gnu.org
2014-09-11 22:26 ` leis at in dot tum.de
2014-09-13  9:36 ` mikpelinux at gmail dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).