optimization/6985: GCC-3.x incorrectly initializes local arrays
lucho@haemimont.bg
lucho@haemimont.bg
Mon Jun 10 17:56:00 GMT 2002
>Number: 6985
>Category: optimization
>Synopsis: GCC-3.x incorrectly initializes local arrays
>Confidential: no
>Severity: serious
>Priority: medium
>Responsible: unassigned
>State: open
>Class: wrong-code
>Submitter-Id: net
>Arrival-Date: Mon Jun 10 17:56:00 PDT 2002
>Closed-Date:
>Last-Modified:
>Originator: Luchezar Belev
>Release: GCC-3.1
>Organization:
>Environment:
linux-2.4.18 pc-i686 glibc-2.1 gcc-3.1
>Description:
It seems that the compiler reorders the initialization-to-zero
with the following assignment operation so that the initialization comes last.
>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;
}
>Fix:
To work around: make little changes to the source and the compiler won't confuse anymore.
>Release-Note:
>Audit-Trail:
>Unformatted:
More information about the Gcc-bugs
mailing list