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...
Fix: The workaround is mentioned in downcase(): move the increment to a separate line (expression).
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.
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!
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.
Reopening to ...
Mark as a dup of bug 11751. *** This bug has been marked as a duplicate of 11751 ***