Compile as
g++ -O2 -m64 h.ii ./a.out
The code should print 2; it does so with GCC 3.4.6, GCC 4.1.2 and GCC 4.2.1.
It prints 1.27688e-161 (or any other random value) with GCC 4.3; valgrind
complains about
==14214== Invalid read of size 8
==14214==at 0x4007F0: HD::e(int) const (in a.out)
==14214== Address 0x4D50028 is 8 bytes before a block of size 8 alloc'd
==14214==at 0x4905665: operator new[](unsigned long)
(vg_replace_malloc.c:199)
Output of g++ -v -O2 -m64 h.ii:
Using built-in specs.
Target: x86_64-unknown-linux-gnu
Configured with: /build/LCG/gcc-4.3.0/configure
--prefix=/afs/cern.ch/sw/lcg/contrib/gcc/4.3.0/slc4_amd64_gcc34
--with-mpfr=/afs/cern.ch/sw/lcg/contrib/mpfr/2.3.1/slc4_amd64_gcc34 :
(reconfigured) /build/LCG/gcc-4.3.0/configure
--prefix=/afs/cern.ch/sw/lcg/contrib/gcc/4.3.0/slc4_amd64_gcc34
--with-mpfr=/afs/cern.ch/sw/lcg/contrib/mpfr/2.3.1/slc4_amd64_gcc34
Thread model: posix
gcc version 4.3.0 (GCC)
COLLECT_GCC_OPTIONS='-v' '-O2' '-m64' '-shared-libgcc' '-mtune=generic'
/afs/cern.ch/sw/lcg/contrib/gcc/4.3.0/slc4_amd64_gcc34/libexec/gcc/x86_64-unknown-linux-gnu/4.3.0/cc1plus
-fpreprocessed h.ii -quiet -dumpbase h.ii -m64 -mtune=generic -auxbase h -O2
-version -o /tmp/axel/ccFq5mLH.s
GNU C++ (GCC) version 4.3.0 (x86_64-unknown-linux-gnu)
compiled by GNU C version 4.3.0, GMP version 4.1.4, MPFR version 2.3.1.
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: a1d58d96913e85190a50d3b74e2cfdbe
COLLECT_GCC_OPTIONS='-v' '-O2' '-m64' '-shared-libgcc' '-mtune=generic'
as -V -Qy --64 -o /tmp/axel/cc6ubWDk.o /tmp/axel/ccFq5mLH.s
GNU assembler version 2.15.92.0.2 (x86_64-redhat-linux) using BFD version
2.15.92.0.2 20040927
COMPILER_PATH=/afs/cern.ch/sw/lcg/contrib/gcc/4.3.0/slc4_amd64_gcc34/libexec/gcc/x86_64-unknown-linux-gnu/4.3.0/:/afs/cern.ch/sw/lcg/contrib/gcc/4.3.0/slc4_amd64_gcc34/libexec/gcc/x86_64-unknown-linux-gnu/4.3.0/:/afs/cern.ch/sw/lcg/contrib/gcc/4.3.0/slc4_amd64_gcc34/libexec/gcc/x86_64-unknown-linux-gnu/:/afs/cern.ch/sw/lcg/contrib/gcc/4.3.0/slc4_amd64_gcc34/lib/gcc/x86_64-unknown-linux-gnu/4.3.0/:/afs/cern.ch/sw/lcg/contrib/gcc/4.3.0/slc4_amd64_gcc34/lib/gcc/x86_64-unknown-linux-gnu/
LIBRARY_PATH=/afs/cern.ch/sw/lcg/contrib/gcc/4.3.0/slc4_amd64_gcc34/lib/gcc/x86_64-unknown-linux-gnu/4.3.0/:/afs/cern.ch/sw/lcg/contrib/gcc/4.3.0/slc4_amd64_gcc34/lib/gcc/x86_64-unknown-linux-gnu/4.3.0/../../../../lib64/:/lib/../lib64/:/usr/lib/../lib64/:/afs/cern.ch/sw/lcg/contrib/gcc/4.3.0/slc4_amd64_gcc34/lib/gcc/x86_64-unknown-linux-gnu/4.3.0/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-O2' '-m64' '-shared-libgcc' '-mtune=generic'
/afs/cern.ch/sw/lcg/contrib/gcc/4.3.0/slc4_amd64_gcc34/libexec/gcc/x86_64-unknown-linux-gnu/4.3.0/collect2
--eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2
/usr/lib/../lib64/crt1.o /usr/lib/../lib64/crti.o
/afs/cern.ch/sw/lcg/contrib/gcc/4.3.0/slc4_amd64_gcc34/lib/gcc/x86_64-unknown-linux-gnu/4.3.0/crtbegin.o
-L/afs/cern.ch/sw/lcg/contrib/gcc/4.3.0/slc4_amd64_gcc34/lib/gcc/x86_64-unknown-linux-gnu/4.3.0
-L/afs/cern.ch/sw/lcg/contrib/gcc/4.3.0/slc4_amd64_gcc34/lib/gcc/x86_64-unknown-linux-gnu/4.3.0/../../../../lib64
-L/lib/../lib64 -L/usr/lib/../lib64
-L/afs/cern.ch/sw/lcg/contrib/gcc/4.3.0/slc4_amd64_gcc34/lib/gcc/x86_64-unknown-linux-gnu/4.3.0/../../..
/tmp/axel/cc6ubWDk.o -lstdc++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc
/afs/cern.ch/sw/lcg/contrib/gcc/4.3.0/slc4_amd64_gcc34/lib/gcc/x86_64-unknown-linux-gnu/4.3.0/crtend.o
/usr/lib/../lib64/crtn.o
Please change the summary if you can think of anything more appropriate.
--
Summary: [4.3 regression] returning result of sqrt() can cause
wrong return value, invalid reads
Product: gcc
Version: 4.3.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: gcc at axel-naumann dot de
GCC build triplet: x86_64-unknown-linux-gnu
GCC host triplet: x86_64-unknown-linux-gnu
GCC target triplet: x86_64-unknown-linux-gnu
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=36045