GCC 5 optimizer produces incorrect code

Bug #1662495 reported by Egor Suvorov
14
This bug affects 3 people
Affects Status Importance Assigned to Milestone
gcc-5 (Ubuntu)
Confirmed
Undecided
Unassigned
gcc-7 (Ubuntu)
Fix Released
Undecided
Unassigned

Bug Description

Inspired by the following blog post: http://codeforces.com/blog/entry/50291

Attached file is a snippet of code which, when compiled with `g++ a.cpp` produces an executable `a.out`, which runs correctly. But when compiled with `g++ a.cpp -O1` the resulting executable crashes in line 10 (according to GDB). Looks like array out-of-bounds error happens despite corresponding check being present in the code.

We was able to reproduce it on two independent installations of Ubuntu 16.04, compiler version is `gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.4)` (looks like it's the most recent package available as of February 7, 2017).

Steps to reproduce:
1. Download `a.cpp` attached.
2. Run `g++ a.cpp -O1` and ensure that it finished without any errors.
3. You can optionally run `g++ a.cpp -O1 -Wall -Wextra -Werror -pedantic` and ensure that this command finishes without any errors as well.
4. Run `./a.out`.

Expected behavior: program finishes correctly.

Real behavior: program crashes with segmentation fault.

Revision history for this message
Egor Suvorov (yeputons) wrote :
Revision history for this message
Launchpad Janitor (janitor) wrote :

Status changed to 'Confirmed' because the bug affects multiple users.

Changed in gcc-5 (Ubuntu):
status: New → Confirmed
Revision history for this message
Egor Suvorov (yeputons) wrote :

Sidenote: we were unable to reproduce that bug on:
1. Ideone (they claim to use GCC 5.1, but do not specify which flavor it is).
2. tdm-gcc 5.1.0 on Windows.
3. gcc version 6.1.0 (Gentoo 6.1.0 p1.0)

And we were able to reproduce it on (in addition to what's mentioned in the report):
1. g++ (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4
2. gcc version 5.4.1 20160904 (Ubuntu 5.4.1-2ubuntu1~14.04)
3. gcc version 6.2.0 20160901 (Ubuntu 6.2.0-3ubuntu11~14.04)

That's why it's submitted to Ubuntu's bugtracker first.

Revision history for this message
Matthias Klose (doko) wrote :

fixed in gcc-7

Changed in gcc-7 (Ubuntu):
status: New → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Bug attachments

Remote bug watches

Bug watches keep track of this bug in other bug trackers.