User account creation filtered due to spam.

Bug 46443 - -O2 flag generates incorrect code
Summary: -O2 flag generates incorrect code
Status: RESOLVED DUPLICATE of bug 21920
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 4.1.2
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-11-11 23:47 UTC by Arthur Muller
Modified: 2010-11-12 00:09 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments
Comment and uncomment the printf statement for different behavior with -O2 (201 bytes, text/x-c)
2010-11-11 23:47 UTC, Arthur Muller
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Arthur Muller 2010-11-11 23:47:50 UTC
Created attachment 22379 [details]
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
Comment 1 Andrew Pinski 2010-11-11 23:53:31 UTC
You are violating C/C++ aliasing rules; You are accessing a double as a long which causes undefined behavior.  memcpy does not cause this violation since it is defined to use the character type to do the copy.

*** This bug has been marked as a duplicate of bug 21920 ***
Comment 2 Arthur Muller 2010-11-12 00:09:27 UTC
(In reply to comment #1)
> You are violating C/C++ aliasing rules; You are accessing a double as a long
> which causes undefined behavior.  memcpy does not cause this violation since it
> is defined to use the character type to do the copy.
> 
> *** This bug has been marked as a duplicate of bug 21920 ***

Thanks for the clarification!

-Arthur