[Bug c/50606] gcc fails to detect obvious use of NULL pointer

manu at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Mon May 14 19:40:00 GMT 2012


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50606

--- Comment #3 from Manuel López-Ibáñez <manu at gcc dot gnu.org> 2012-05-14 18:47:01 UTC ---
(In reply to comment #2)
> (In reply to comment #1)
> > printf warnings are handled in the FE, so we would need constant-propagation in
> > the FE. Clang implements it, so I know it is possible. 
> 
> I think my bug report wasn't clearly worded. 
> printf was merely an example function. 
> I am interested in having gcc track when it knows
> for certain that a pointer is NULL, so that it
> can detect uses that won't work and emit a warning.
>

There are two ways to implement this in such a general way. 

1) A new pass in the middle-end. You can look at the implementation of nonnull,
and try to figure out something similar. The advantage is that you can make
full use of middle-end capabilities. The disadvantage is that optimization
passes may hide obvious warnings, and the warnings will require optimization to
be enabled.

2) Purely in the FE. This will require substantially more work, since you have
to implement some kind of conditional constant propagation, but it will be far
more reliable and work without optimizations.

But any of the above would be better than nothing, so choose whatever seems
better for you. It is a bit of a shame that GCC doesn't warn even for very
simple cases:

void f( const char * p)
{
  if (p == 0)
    __builtin_printf(*p);
}



More information about the Gcc-bugs mailing list