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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Michael Haubenwallner wrote:

> > 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 :)

You have to pay a small amount to get an official copy of the standard
but there are several draft copies available for free.  See
<http://www.iso-9899.info/wiki/The_Standard>,
<http://www.open-std.org/jtc1/sc22/wg14/>,
<http://gcc.gnu.org/readings.html>, etc.

For details on aliasing, there is some discussion of it here
<http://gcc.gnu.org/bugs.html#nonbugs_c> which includes a link to
<http://mail-index.netbsd.org/tech-kern/2003/08/11/0001.html>.

Your code violates the aliasing rules of C99 by accessing type (char *)
by dereferencing a (void **) cast to (char **).  The standard says you
cannot do this.  Previous versions of gcc were not as strict at
enforcing these rules, so you could get away with it then, but not any
more.  The standard was specifically written this way to make it
possible for the compiler to perform a certain set of optimizations
which would not otherwise be possible, and gcc is utilizing this
freedom.  It is thus a bug in your code because your code is not valid
C.

You might be tempted to consider it a bug in the ISO/IEC committees for
allowing this in the standard, but that's not really a practical
standpoint as the decision has been made (close to a decade ago now) and
there's no good in arguing it now.

You might be tempted to consider this a bug in gcc for breaking code
that used to work, but again, gcc is breaking code that's invalid, which
the standard specifically allows it to do.  And besides, they gave you
an out with "-fno-strict-aliasing" so that you can get the old behavior
if you really want it.

Also note that this discussion ("it's broken code" / "no, it's code that
used to work") has already played out at length on the gcc development
list, to the point of being somewhat of a sore spot now, and pretty much
everyone's mind has been made up.

Brian

[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux