This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: bad user, or optimization bug with worked-around lvalue casts ?


On Mon, 2007-01-08 at 15:39 +0000, Andrew Haley wrote:
> Michael Haubenwallner writes:
>  > Hi,
>  > 
>  > after switching to gcc-4.1.1 from gcc-3.4.5, I'm facing a problem with
>  > this code searching for non-blank in a string (extracted test-case):
>  > 
>  > #include <stdio.h>
>  > int check(const void *pvBuf)
>  > {
>  >     for(; *((char*)pvBuf) == ' '; ++ *((char**)&pvBuf));
> 
> This is wrong.  Try this:
> 
> for(; *((char*)pvBuf) == ' '; pvBuf = (char*)pvbuf + 1);

Yep, have my code changed like this, thanks.

> 
>  >     return *((char*)pvBuf);
>  > }
>  > int main(void)
>  > {
>  >         printf("%d\n", check(" x"));
>  >         return 0;
>  > }
>  > 
>  > When building without optimization, it works as expected.
>  > When building with -O2 or higher, the loop hangs.
>  > This seems to be platform independant, as it occurs on x86-linux as well
>  > as powerpc-aix, hppa-hpux, ia64-hpux, x86-solaris and sparc-solaris.
>  > 
>  > Now the question is:
>  > Is this a user-bug only, not looking at the output of "-Wall" (because
>  > this code builds and works successfully for 5 years now),
>  > or is it a gcc-bug too, producing working code without optimization
>  > and non-working code with optimization level >= 2 ?
>  > 
>  > And: can this code be changed without using an intermediate variable ?
> 
> This is an aliasing bug.  Please see The C standard, Section 6.3.2.3,
> Pointers.

Hmm, a bug in what product - the standard, gcc, or my application ?

And - sorry for that question - where do one read the standard from ?
I've found C89, this seems to have only 5 sections...
And in the C99 draft, Section 6.3.2.3 has to do with aliasing, but I'm
completely lost here - I'll better let interpreting the standard up to
you :)

> 
> Alternatively, you may compile your code with -fno-strict-aliasing.

Yes, this works too, thanks.

/haubi/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]