http://gcc.gnu.org/bugzilla/show_bug.cgi?id=46443
Summary: -O2 flag generates incorrect code
Product: gcc
Version: 4.1.2
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
AssignedTo: [email protected]
ReportedBy: [email protected]
Created attachment 22379
--> http://gcc.gnu.org/bugzilla/attachment.cgi?id=22379
Comment and uncomment the printf statement for different behavior with -O2
I'm attaching a small file bug.c. If you compile with
gcc -c -O2 bug.c
gcc -o bug.x bug.o
then the executable bug.x has problems. This can be verified simply by running
valgrind on the executable. It complains about uninitialized variables.
I you uncomment the printf statement from bug.c then the problem goes away.
I have a workaround in my real code by replacing the pointer variable "ua" by a
non-pointer variable "ua" and using memcpy(&ua,&a,sizeof(double)) to initialize
ua. So even though gcc is generating incorrect code I don't think this is
critical as there's a workaround for it.
-Arthur
PS: Here is the valgrind output for when the printf statement is not there
==7128== Memcheck, a memory error detector
==7128== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al.
==7128== Using Valgrind-3.5.0 and LibVEX; rerun with -h for copyright info
==7128== Command: bug.x
==7128==
==7128== Use of uninitialised value of size 8
==7128== at 0x373B8419BD: _itoa_word (in /lib64/libc-2.5.so)
==7128== by 0x373B844E5A: vfprintf (in /lib64/libc-2.5.so)
==7128== by 0x373B84D3F9: printf (in /lib64/libc-2.5.so)
==7128== by 0x4005FE: main (in
/home/vki/muller/Gnubuild/src/base/exam/bug.x)
==7128==
==7128== Conditional jump or move depends on uninitialised value(s)
==7128== at 0x373B8419C7: _itoa_word (in /lib64/libc-2.5.so)
==7128== by 0x373B844E5A: vfprintf (in /lib64/libc-2.5.so)
==7128== by 0x373B84D3F9: printf (in /lib64/libc-2.5.so)
==7128== by 0x4005FE: main (in
/home/vki/muller/Gnubuild/src/base/exam/bug.x)
==7128==
==7128== Conditional jump or move depends on uninitialised value(s)
==7128== at 0x373B844ED4: vfprintf (in /lib64/libc-2.5.so)
==7128== by 0x373B84D3F9: printf (in /lib64/libc-2.5.so)
==7128== by 0x4005FE: main (in
/home/vki/muller/Gnubuild/src/base/exam/bug.x)
==7128==
==7128== Conditional jump or move depends on uninitialised value(s)
==7128== at 0x373B84563F: vfprintf (in /lib64/libc-2.5.so)
==7128== by 0x373B84D3F9: printf (in /lib64/libc-2.5.so)
==7128== by 0x4005FE: main (in
/home/vki/muller/Gnubuild/src/base/exam/bug.x)
==7128==
==7128== Conditional jump or move depends on uninitialised value(s)
==7128== at 0x373B843B60: vfprintf (in /lib64/libc-2.5.so)
==7128== by 0x373B84D3F9: printf (in /lib64/libc-2.5.so)
==7128== by 0x4005FE: main (in
/home/vki/muller/Gnubuild/src/base/exam/bug.x)
==7128==
return value = 2