[PR42542] g++ -O3 produces incorrect results for less-than operator

Bug #403744 reported by Nathan Bell
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
gcc
Fix Released
Medium
gcc-4.3 (Ubuntu)
Won't Fix
Undecided
Unassigned
gcc-4.4 (Ubuntu)
Fix Released
Undecided
Unassigned

Bug Description

Binary package hint: g++-4.3

g++ appears to be (incorrectly) using a signed int comparison when -O3 optimization is enabled. Compiling with -O2 or lower produces the correct output.

This problem was observed on Ubuntu 9.04 x86_64. The source code (bug.cpp) is attached.

$ lsb_release -rd
Description: Ubuntu 9.04
Release: 9.04

$ uname -a
Linux rabota 2.6.28-13-generic #45-Ubuntu SMP Tue Jun 30 22:12:12 UTC 2009 x86_64 GNU/Linux

$ apt-cache policy g++-4.3
g++-4.3:
  Installed: 4.3.3-5ubuntu4
  Candidate: 4.3.3-5ubuntu4
  Version table:
 *** 4.3.3-5ubuntu4 0
        500 http://us.archive.ubuntu.com jaunty/main Packages
        100 /var/lib/dpkg/status

$ g++ --version
g++ (Ubuntu 4.3.3-5ubuntu4) 4.3.3

$ g++ -O2 bug.cpp && ./a.out
4264598888

$ g++ -O3 bug.cpp && ./a.out
1898580612

$ g++ -v -O3 bug.cpp
Using built-in specs.
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.3.3-5ubuntu4' --with-bugurl=file:///usr/share/doc/gcc-4.3/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --enable-shared --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --enable-nls --with-gxx-include-dir=/usr/include/c++/4.3 --program-suffix=-4.3 --enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --enable-mpfr --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.3.3 (Ubuntu 4.3.3-5ubuntu4)
COLLECT_GCC_OPTIONS='-v' '-O3' '-shared-libgcc' '-mtune=generic'
 /usr/lib/gcc/x86_64-linux-gnu/4.3.3/cc1plus -quiet -v -D_GNU_SOURCE bug.cpp -D_FORTIFY_SOURCE=2 -quiet -dumpbase bug.cpp -mtune=generic -auxbase bug -O3 -version -fstack-protector -o /tmp/cc1tnMId.s
ignoring nonexistent directory "/usr/local/include/x86_64-linux-gnu"
ignoring nonexistent directory "/usr/lib/gcc/x86_64-linux-gnu/4.3.3/../../../../x86_64-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/include/c++/4.3
 /usr/include/c++/4.3/x86_64-linux-gnu
 /usr/include/c++/4.3/backward
 /usr/local/include
 /usr/lib/gcc/x86_64-linux-gnu/4.3.3/include
 /usr/lib/gcc/x86_64-linux-gnu/4.3.3/include-fixed
 /usr/include/x86_64-linux-gnu
 /usr/include
End of search list.
GNU C++ (Ubuntu 4.3.3-5ubuntu4) version 4.3.3 (x86_64-linux-gnu)
 compiled by GNU C version 4.3.3, GMP version 4.2.4, MPFR version 2.4.0.
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: d9dd67dcc191271ac69fd2c05266cf9c
COLLECT_GCC_OPTIONS='-v' '-O3' '-shared-libgcc' '-mtune=generic'
 as -V -Qy -o /tmp/ccZsdHhv.o /tmp/cc1tnMId.s
GNU assembler version 2.19.1 (x86_64-linux-gnu) using BFD version (GNU Binutils for Ubuntu) 2.19.1
COMPILER_PATH=/usr/lib/gcc/x86_64-linux-gnu/4.3.3/:/usr/lib/gcc/x86_64-linux-gnu/4.3.3/:/usr/lib/gcc/x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/4.3.3/:/usr/lib/gcc/x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/4.3.3/:/usr/lib/gcc/x86_64-linux-gnu/
LIBRARY_PATH=/usr/lib/gcc/x86_64-linux-gnu/4.3.3/:/usr/lib/gcc/x86_64-linux-gnu/4.3.3/:/usr/lib/gcc/x86_64-linux-gnu/4.3.3/../../../../lib/:/lib/../lib/:/usr/lib/../lib/:/usr/lib/gcc/x86_64-linux-gnu/4.3.3/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-O3' '-shared-libgcc' '-mtune=generic'
 /usr/lib/gcc/x86_64-linux-gnu/4.3.3/collect2 --eh-frame-hdr -m elf_x86_64 --hash-style=both -dynamic-linker /lib64/ld-linux-x86-64.so.2 -z relro /usr/lib/gcc/x86_64-linux-gnu/4.3.3/../../../../lib/crt1.o /usr/lib/gcc/x86_64-linux-gnu/4.3.3/../../../../lib/crti.o /usr/lib/gcc/x86_64-linux-gnu/4.3.3/crtbegin.o -L/usr/lib/gcc/x86_64-linux-gnu/4.3.3 -L/usr/lib/gcc/x86_64-linux-gnu/4.3.3 -L/usr/lib/gcc/x86_64-linux-gnu/4.3.3/../../../../lib -L/lib/../lib -L/usr/lib/../lib -L/usr/lib/gcc/x86_64-linux-gnu/4.3.3/../../.. /tmp/ccZsdHhv.o -lstdc++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc /usr/lib/gcc/x86_64-linux-gnu/4.3.3/crtend.o /usr/lib/gcc/x86_64-linux-gnu/4.3.3/../../../../lib/crtn.o

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

it's a bug in your test case, result is overflowing. use `long' instead

Changed in gcc-4.3 (Ubuntu):
status: New → Invalid
Revision history for this message
Nathan Bell (wnbell) wrote :

All of those numbers are less than 4294967296 (i.e. 2^32), so they can be stored in 32-bit *unsigned* integers.

Nevermind that "overflow" refers to the result of an arithmetic operation, and no arithmetic operations are being used here.

Please don't dismiss this issue with nothing more than a cursory look. I'm quite confident this is a bug.

Changed in gcc-4.3 (Ubuntu):
status: Invalid → New
Changed in gcc:
status: Unknown → New
Matthias Klose (doko)
summary: - g++ -O3 produces incorrect results for less-than operator
+ [PR42542] g++ -O3 produces incorrect results for less-than operator
Matthias Klose (doko)
Changed in gcc-4.3 (Ubuntu):
status: New → Triaged
Changed in gcc:
status: New → Confirmed
Revision history for this message
Launchpad Janitor (janitor) wrote :

This bug was fixed in the package gcc-4.4 - 4.4.2-9ubuntu1

---------------
gcc-4.4 (4.4.2-9ubuntu1) lucid; urgency=low

  * Merge with Debian.

gcc-4.4 (4.4.2-9) unstable; urgency=low

  [ Matthias Klose ]
  * Update to SVN 20100108 from the gcc-4_4-branch (r155727).
    - Fixes PR tree-optimization/42337, PR target/42448, PR middle-end/42099,
      PR target/42549 (closes: #560812), PR tree-optimization/41956,
      PR target/42503, PR other/42611 (LP: #501921), PR target/42511,
      PR target/42564, PR tree-optimization/42614,
      PR target/42542 (LP: #403744).
  * libgcj10: Move .so symlinks into gcj-4.4-jdk. Addresses: #563280.
  * Don't use expect-tcl8.3 on hppa anymore.
  * Merge gnat-4.4 changes back from 4.4.2-5.
  * Default to v9a (ultrasparc) on sparc*-linux.
  * gcj-4.4-jdk: Include /usr/lib/jvm-exports.
  * Use 4.4.3 as the upstream version (we'll end up with this version
    for the next release anyway).
  * Always build libgcc4 on hppa, even if not building libgcc for other
    archs (gcc-4.5 bumps the soversion on hppa to 6).
  * Base the source build-dependency on the package version instead of the
    gcc version.
  * Build-depend on autogen again.

  [ Nobuhiro Iwamatsu ]
  * Update gcc-multiarch patch for sh4.
 -- Matthias Klose <email address hidden> Fri, 08 Jan 2010 08:47:20 +0100

Changed in gcc-4.4 (Ubuntu):
status: New → Fix Released
Revision history for this message
Nathan Bell (wnbell) wrote :

Thanks Matthias!

Changed in gcc:
status: Confirmed → Fix Released
Revision history for this message
Matthias Klose (doko) wrote :

closing the 4.3 task as won't fix. fixed in lucid.

Changed in gcc-4.3 (Ubuntu):
status: Triaged → Won't Fix
Changed in gcc:
importance: Unknown → Medium
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.