Bug 6985 - GCC-3.x incorrectly initializes local arrays
Summary: GCC-3.x incorrectly initializes local arrays
Status: RESOLVED DUPLICATE of bug 21920
Alias: None
Product: gcc
Classification: Unclassified
Component: rtl-optimization (show other bugs)
Version: 3.1
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2002-06-10 17:56 UTC by lucho
Modified: 2005-11-10 03:51 UTC (History)
49 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 lucho 2002-06-10 17:56:00 UTC
It seems that the compiler reorders the initialization-to-zero
with the following assignment operation so that the initialization comes last.

Release:
GCC-3.1

Environment:
linux-2.4.18 pc-i686 glibc-2.1 gcc-3.1

How-To-Repeat:
/*
 * GCC-3.1 bug
 * compile with -O2 to let the bug to appear.
 *
 * The test should output (and does so when compiled with -O1 or less):
 *
 * aaaaa
 * a=13
 *
 * but when compiled with -O2 or higher, instead it outputs:
 *
 * aaaaa
 * a=0
 *
 *
 * I've tested this on 2.95.2, 3.0, 3.0.4 and 3.1.
 * 2.95.2 doesn't have this bug and all 3.x have it.
 */

unsigned a = 13, b = 17;

int main()
{
	unsigned char buf[18] = {0};
	*(unsigned short *)(buf + 12) = a;
	*(unsigned short *)(buf + 14) = b;
	printf("aaaaa\n");
	printf("a=%i\n", *(unsigned short *)(buf + 12));
	return 0;
}
Comment 1 lucho 2002-06-10 17:56:00 UTC
Fix:
To work around: make little changes to the source and the compiler won't confuse anymore.
Comment 2 Andrew Pinski 2002-06-10 21:22:47 UTC
From: Andrew Pinski <pinskia@physics.uc.edu>
To: lucho@haemimont.bg
Cc: gcc-gnats@gcc.gnu.org
Subject: Re: optimization/6985: GCC-3.x incorrectly initializes local arrays
Date: Mon, 10 Jun 2002 21:22:47 -0400

 This is not a bug but your code violates ISO C's aliasing rules 
 (C89, ANSI C).
 To work around this use an union or use the option 
 `-fno-strict-aliasing'.
 Thanks,
 Andrew Pinski
 
 On Monday, June 10, 2002, at 08:48 , lucho@haemimont.bg wrote:
 
 > /*
 >  * GCC-3.1 bug
 >  * compile with -O2 to let the bug to appear.
 >  *
 >  * The test should output (and does so when compiled with -O1 or less):
 >  *
 >  * aaaaa
 >  * a=13
 >  *
 >  * but when compiled with -O2 or higher, instead it outputs:
 >  *
 >  * aaaaa
 >  * a=0
 >  *
 >  *
 >  * I've tested this on 2.95.2, 3.0, 3.0.4 and 3.1.
 >  * 2.95.2 doesn't have this bug and all 3.x have it.
 >  */
 >
 > unsigned a = 13, b = 17;
 >
 > int main()
 > {
 > 	unsigned char buf[18] = {0};
 > 	*(unsigned short *)(buf + 12) = a;
 > 	*(unsigned short *)(buf + 14) = b;
 > 	printf("aaaaa\n");
 > 	printf("a=%i\n", *(unsigned short *)(buf + 12));
 > 	return 0;
 > }
 
Comment 3 Volker Reichelt 2002-11-24 14:33:19 UTC
State-Changed-From-To: open->closed
State-Changed-Why: Not a bug, but a violation of aliasing rules in the user code.
    See audit trail.
Comment 4 Andrew Pinski 2005-11-10 03:51:16 UTC
Reopening to ...
Comment 5 Andrew Pinski 2005-11-10 03:51:35 UTC
Mark as a dup of bug 21920.

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