From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 19351 invoked by alias); 11 Sep 2014 21:15:00 -0000 Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-bugs-owner@gcc.gnu.org Received: (qmail 19320 invoked by uid 48); 11 Sep 2014 21:14:57 -0000 From: "leis at in dot tum.de" To: gcc-bugs@gcc.gnu.org Subject: [Bug c/63233] New: Valid out of bounds access leads to undefined behavior Date: Thu, 11 Sep 2014 21:15:00 -0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c X-Bugzilla-Version: 4.9.1 X-Bugzilla-Keywords: X-Bugzilla-Severity: major X-Bugzilla-Who: leis at in dot tum.de X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 7bit X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-SW-Source: 2014-09/txt/msg01453.txt.bz2 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 #include 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.