Article View: gnu.gcc.bug
Article #5998bug!
From: Kaz Kylheku
Date: Thu, 26 Dec 1996 00:00
Date: Thu, 26 Dec 1996 00:00
46 lines
2064 bytes
2064 bytes
/*
* This program demonstrates a subtle oddity of the GNU C Compiler.
*
* Functions foo and bar return expressions that are equivalent, except
* that inside foo, the expression is parenthesized as:
*
* (A && B) && (A && B)
*
* whereas in bar, the expression is parenthesized as
*
* A && B && A && B
*
* The expression B has a side effect which affects the evaluation of
* a subsequent A or B. When GCC (@1) translates
* foo (@2), it seems to perform a transformation that is roughly equivalent to:
*
* A && B && B
*
* That is, the condition tested by A is only evaluated once---only one
* test and branch is generated. This evaluation appears to seriously violate
* ANSI/ISO C, which requires the left constituent of the top-level && to be
* evaluated completely and its side effects settled (@3), followed by the
* evaluation of the right constituent. A recursive application of the ANSI
* rule ensures that the order is the same regardless of the presence of
* the extra parentheses, and hence the above optimization is incorrect.
* -----
* @1. GCC v. 2.7.2 targeted for 80x86. Not tested with other targets.
* @2. Translation done with -Wall -ansi -O2 -S.
* @3. ISO/IEC 9899:1990(E), Sec 6.3.13 ``Logical And Operator''.
*/
int foo(int x, int y, char *z, int c)
{
return ((x < y) && (z[x++] = c, 1))
&& ((x < y) && (z[x++] = c, 1));
}
int bar(int x, int y, char *z, int c)
{
return (x < y) && (z[x++] = c, 1)
&& (x < y) && (z[x++] = c, 1);
}
Technical Details
Message-ID:
<Pine.LNX.3.95.961226122105.390A-100000@bullwinkle>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 113d02,f1d95c24fa8e7144,start X-Google-Attributes: gid113d02,public From: kaz@cafe.net (Kaz Kylheku) Subject: bug! Date: 1996/12/26 Message-ID: <Pine.LNX.3.95.961226122105.390A-100000@bullwinkle> X-Deja-AN: 206082559 sender: bug-gcc-request@prep.ai.mit.edu x-sender: kaz@bullwinkle content-type: TEXT/PLAIN; charset=US-ASCII x-gateway: relay4.UU.NET from bug-gcc to gnu.gcc.bug; Thu, 26 Dec 1996 15:22:28 EST mime-version: 1.0 newsgroups: gnu.gcc.bug