Bug 1910 - incorrect long long return value with -O2 optimization
incorrect long long return value with -O2 optimization
Status: RESOLVED DUPLICATE of bug 21920
Product: gcc
Classification: Unclassified
Component: c
2.97
: P3 normal
: ---
Assigned To: Not yet assigned to anyone
: wrong-code
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2001-02-08 05:16 UTC by djk
Modified: 2005-06-05 09:29 UTC (History)
4 users (show)

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


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description djk 2001-02-08 05:16:02 UTC
-O2 generates incorrect code and causes the following
example routine to return a long long value from an
uninitialized stack location.

struct cl {
        int one, two;
};
const char *error = "edx should get 0, eax should get ebp[4]";
long long broken (int a){
        struct cl val;
        val.one = a;
        val.two = 0;

        return *(long long *)&val;
}

--------------
the codesourcery compile server gave this answer today:
        .file   "@5412.7.c"
        .version        "01.01"
gcc2_compiled.:
.globl error
        .section        .rodata
        .align 32
.LC0:
        .string "edx should get 0, eax should get ebp[4]"
        .data
        .align 4
        .type   error,@object
        .size   error,4
error:
        .long   .LC0
        .text
        .align 4
.globl broken
        .type   broken,@function
broken:
        pushl   %ebp
        movl    %esp, %ebp
        subl    $8, %esp
        movl    -8(%ebp), %eax
        movl    -4(%ebp), %edx
        leave
        ret
.Lfe1:
        .size   broken,.Lfe1-broken
        .ident  "GCC: (GNU) 2.97 20010207 (experimental)"

------------------------

Looks like the optimizer removed the two assignments to
the structure (one is a parameter, the other is a zero).

Release:
GCC: (GNU) 2.97 20010207 (experimental)

Environment:
intel

How-To-Repeat:
gcc -O2 -S bug.c
inspect eax & edx values returned from function.
Comment 1 Richard Henderson 2001-02-09 00:31:59 UTC
From: Richard Henderson <rth@redhat.com>
To: djk@super.org
Cc: gcc-gnats@gcc.gnu.org
Subject: Re: c/1910: incorrect long long return value with -O2 optimization
Date: Fri, 9 Feb 2001 00:31:59 -0800

 On Thu, Feb 08, 2001 at 01:14:45PM -0000, djk@super.org wrote:
 >         struct cl val;
 >         val.one = a;
 >         val.two = 0;
 >         return *(long long *)&val;
 
 This code is incorrect.  You are running afoul of ISO C
 type aliasing rules.
 
 
 r~
Comment 2 Neil Booth 2001-02-09 02:31:29 UTC
State-Changed-From-To: open->closed
State-Changed-Why: Aliasing bug in user code.
Comment 3 djk 2001-02-09 08:13:19 UTC
From: dan kopetzky <djk@super.org>
To: Richard Henderson <rth@redhat.com>
Cc: gcc-gnats@gcc.gnu.org
Subject: Re: c/1910: incorrect long long return value with -O2 optimization
Date: Fri, 09 Feb 2001 08:13:19 -0500

 Richard Henderson wrote:
 
 > On Thu, Feb 08, 2001 at 01:14:45PM -0000, djk@super.org wrote:
 > >         struct cl val;
 > >         val.one = a;
 > >         val.two = 0;
 > >         return *(long long *)&val;
 >
 > This code is incorrect.  You are running afoul of ISO C
 > type aliasing rules.
 >
 > r~
 
 Thanks for your prompt reply. I'll convert the offending code to use
 unions.
Comment 4 Neil Booth 2001-02-09 10:31:29 UTC
From: neil@gcc.gnu.org
To: djk@super.org, gcc-gnats@gcc.gnu.org, nobody@gcc.gnu.org
Cc:  
Subject: Re: c/1910
Date: 9 Feb 2001 10:31:29 -0000

 Synopsis: incorrect long long return value with -O2 optimization
 
 State-Changed-From-To: open->closed
 State-Changed-By: neil
 State-Changed-When: Fri Feb  9 02:31:29 2001
 State-Changed-Why:
     Aliasing bug in user code.
 
 http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view&pr=1910&database=gcc

Comment 5 Andrew Pinski 2005-06-05 09:29:37 UTC
Reopening to ...
Comment 6 Andrew Pinski 2005-06-05 09:29:55 UTC
Mark as a dup of bug 21920.

*** This bug has been marked as a duplicate of 21920 ***