Bug 6765

Summary: gcc - incrementation - ternary operator
Product: gcc Reporter: jompo
Component: cAssignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED DUPLICATE    
Severity: normal CC: algorithmus, arminu, asokumar, bala, barnarr, bmead15, buergel, carpman, ceniza666, chuchunxin, devnull, d_picco, eric.mcvicker, gaurav_har, gcc-bugs, gcc, ggs, horsh, jandres, janis, jompo, krs, lid, lindahlb, lxg8906, mikaldaz, nakkore, neil, pierre.van.de.laar_at_philips.com, qyang, raoulgough, rglan, rjvbertin, robc, s9322036, smartmouse714, suan, super.aorta, svetozarmarkov, tczarnecki, vanveghel, work, zshao
Priority: P3 Keywords: wrong-code
Version: 3.0   
Target Milestone: ---   
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed:

Description jompo 2002-05-22 04:26:01 UTC
the ternary operator causes incrementation of left hand side expression to occurs before it should in an expression where the variable appears at both side of the affectation expression.
example: *s++ = (*s == 'x') ?  'X': *s;
Note: this does not appears in gcc-2.96 (Mandrake 8.2)

Release:
gcc 2.95.2 - gcc 2.95.3 - gcc 3.04

Environment:
gcc 2.95.3, gcc 3.04 (home compiled on Linux Mandrake 8.1)
Mac OS X : gcc 2.95.2

How-To-Repeat:
#include <stdio.h>
#define STRING "abc"
int main()
{
   char s[10], *t;

   strcpy(s, STRING);
   t = s;
   while (*t != '\0')
    *t++ = (*t == 'b') ? 'X': *t;
 
        /*  this print "Xc" instead of "aXc"  */
   printf("%s\n",s);
}
Comment 1 jompo 2002-05-22 04:26:01 UTC
Fix:
Must replace ternary operator by if/else instruction
Comment 2 Neil Booth 2002-05-22 05:03:03 UTC
State-Changed-From-To: open->closed
State-Changed-Why: Not a bug. The standard states that whether the LHS or the RHS of '=' is evaluated first is undefined.  You were lucky before.
Comment 3 Andrew Pinski 2005-11-10 03:48:57 UTC
Reopening to ...
Comment 4 Andrew Pinski 2005-11-10 03:49:06 UTC
a dup of bug 11751

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