Bug 9675 - store-and-increment becomes increment-and-store
Summary: store-and-increment becomes increment-and-store
Status: RESOLVED DUPLICATE of bug 11751
Alias: None
Product: gcc
Classification: Unclassified
Component: rtl-optimization (show other bugs)
Version: 3.2.2
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2003-02-12 18:46 UTC by rjvbertin
Modified: 2005-04-20 03:05 UTC (History)
4 users (show)

See Also:
Host: i686-pc-linux-gnu
Target: i686-pc-linux-gnu
Build: i686-pc-linux-gnu
Known to work:
Known to fail:
Last reconfirmed:


Attachments
gcc32xbug20030212.tar.bz2 (35.06 KB, application/octet-stream)
2003-05-21 15:17 UTC, rjvbertin
Details

Note You need to log in before you can comment on or make changes to this bug.
Description rjvbertin 2003-02-12 18:46:00 UTC
In the file below (also in the archive attached), compiling with any form of optimisation via a gcc wrapper script produces code, in downcase(), that does an increment-and-store instead of store-and-increment on the *s++ = rval statement. The result is that the first character of any string is lost. I encountered this problem first in gcc 3.2.0, and it persists in gcc 3.2.2.

Additionally, when the file is compiled by direct evocation of the compiler (gcc32.2 -s -o tdowncase tdowncase.c), the programme crashes on the wrongly compiled line.

The attached archive contains
* tdowncase.c : the source
* gccopt : my wrapper script
* coredumps.tar : archive containing the temp files, binary and coredump for the crashing version, as well as the log of the compilation and execution
* wrongcode.tar : idem for the non-crashing, wrong-result version, with the log



/* tdowncase: a case of wrong optimisation */
#include <stdio.h>
#include <ctype.h>

void downcase( char *s )
{
   while( *s ){
          *s++= tolower( *s );
            /* gcc 3.2.0 linux seems to have a bug in that *s++=tolower(*s) becomes
             \ *++s=tolower(*s) ?!
             */
/*        s++; */
   }
}

main()
{ char *c1= "ABCDE",
       *c2= "efghi";

     printf( "downcase(\"%s\")=\"", c1 ); fflush(stdout);
     downcase(c1);
     printf( "%s\"\n", c1 );
     printf( "downcase(\"%s\")=\"", c2 ); fflush(stdout);
     downcase(c2);
     printf( "%s\"\n", c2 );
}

Release:
3.2.0 and 3.2.2

Environment:
Linux 2.2.13-7mdk PIII, glibc 2.1.3
host: i686-pc-linux-gnu
build: i686-pc-linux-gnu
target: i686-pc-linux-gnu
configured with: ../gcc-3.2.2/configure --prefix=/usr/local/gcc3.2.2 --enable-languages=c,c++,f77,objc --disable-nls --with-system-zlib

How-To-Repeat:
compile and run...
Comment 1 rjvbertin 2003-02-12 18:46:00 UTC
Fix:
The workaround is mentioned in downcase(): move the increment to a separate line (expression).
Comment 2 Richard Earnshaw 2003-02-12 18:54:38 UTC
State-Changed-From-To: open->closed
State-Changed-Why: Not a bug.  You use and increment a variable twice in the same expression.  This is undefined behaviour.
Comment 3 rjvbertin 2003-02-12 21:20:36 UTC
From: "RenE J.V. Bertin" <rjvbertin@hotmail.com>
To: rearnsha@gcc.gnu.org, gcc-bugs@gcc.gnu.org, gcc-prs@gcc.gnu.org,
   josh@joshisanerd.com, nobody@gcc.gnu.org, rjvbertin@hotmail.com,
   gcc-gnats@gcc.gnu.org
Cc:  
Subject: zcuse-me... Re: optimization/9675: store-and-increment becomes increment-and-store
Date: Wed, 12 Feb 2003 21:20:36 +0100

 On 12 Feb 2003 18:54:38 -0000, rearnsha@gcc.gnu.org wrote regarding "Re: optimization/9675:
 store-and-increment becomes increment-and-store"
 
 Of course it crashes: needs -fwritable-strings ...
 
 Sorry!

Comment 4 Richard Earnshaw 2003-02-13 09:40:48 UTC
From: Richard Earnshaw <rearnsha@arm.com>
To: "RenE J.V. Bertin" <rjvbertin@hotmail.com>
Cc: rearnsha@gcc.gnu.org, gcc-bugs@gcc.gnu.org, gcc-prs@gcc.gnu.org,
        josh@joshisanerd.com, nobody@gcc.gnu.org, gcc-gnats@gcc.gnu.org
Subject: Re: optimization/9675: store-and-increment becomes 
 increment-and-store
Date: Thu, 13 Feb 2003 09:40:48 +0000

 > On 12 Feb 2003 18:54:38 -0000, rearnsha@gcc.gnu.org wrote regarding "Re: optimization/9675:
 > store-and-increment becomes increment-and-store"
 > 
 > I'm informing myself a bit more about the undefined issue (the expr. uses the pointed-to value but increments the pointer). HOWEVER, it may interest you to know that if you modify downcase() to 
 > 
 > void downcase( char *s )
 > { char c;
 >    while( *s ){
 > 		c= *s;
 > 		*s++= tolower( c );
 > 	}
 > }
 > 
 > the coredump issue remains (using compilation as shown in coredumps.log). According to your reply, the above does not contain undefined statements, and -Wsequence-point doesn't complain about any.
 > 
 
 The code above is ok.  Are you trying to downcase a string literal?
 
 R.
Comment 5 Andrew Pinski 2005-04-20 03:05:35 UTC
Reopening to ...
Comment 6 Andrew Pinski 2005-04-20 03:05:57 UTC
Mark as a dup of bug 11751.

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