Null pointer dereference and side effects

Alexandre Oliva aoliva@redhat.com
Wed Jul 21 06:44:00 GMT 2004


On Jul 20, 2004, Archie Cobbs <archie@dellroad.org> wrote:

> It appears gcc is completely ignoring the possibility of
> "side effects" when it optimizes away the comparison.

Oh, rats.  That's because the reasoning about dereferencing NULL
invoking undefined behavior still applies.  Since both arms of the if
return the same value, the compare is optimized away, and the
dereference of the pointer can be removed because, if it was NULL or
any other bad pointer, it would invoke undefined behavior, and
otherwise, the dereference would be safe and unused.

I think you really need to make the pointer volatile to prevent the
dereference from being optimized out.

-- 
Alexandre Oliva             http://www.ic.unicamp.br/~oliva/
Red Hat Compiler Engineer   aoliva@{redhat.com, gcc.gnu.org}
Free Software Evangelist  oliva@{lsd.ic.unicamp.br, gnu.org}



More information about the Gcc-help mailing list