[cfe-dev] Warning when comparing address of function or variable with constant?

Douglas Gregor dgregor at apple.com
Tue Jan 3 08:49:57 PST 2012


On Jan 3, 2012, at 8:20 AM, Matt Beaumont-Gay wrote:

> On Tue, Jan 3, 2012 at 06:14, Ed Schouten <ed at 80386.nl> wrote:
>> Hello all,
>> 
>> This morning I fixed a small bug at FreeBSD that involved the following
>> code:
>> 
>>        void
>>        func(struct foo *idx)
>>        {
>> 
>>                if (index == NULL)
>>                        return;
>>                ...
>>        }
>> 
>> The bug in this code is that we should have compared against idx -- not
>> index. This works by accident, as index() is a function provided by our
>> C library (BSD's strchr()).
>> 
>> I think it is hardly ever possible that a function or variable ever
>> resides at address 0, except in kernelspace or when using a hacked
>> run-time linker. Does Clang have a warning for this?
> 
> Yes, -Wbool-conversions:
> 
> $ cat test.cc
> int index();
> void f(int* idx) {
>  if (index)
>    return;
> }
> $ ./build/bin/clang -fsyntax-only -Wbool-conversions test.cc
> test.cc:3:7: warning: address of function 'index' will always evaluate
> to 'true' [-Wbool-conversions]
>  if (index)
>      ^~~~~
> test.cc:3:7: note: prefix with the address-of operator to silence this warning
>  if (index)
>      ^
>      &
> 1 warning generated.


I think it's perfectly reasonable to extend this warning to also complain about comparisons against null pointers (with the typical caveat for weak declarations).

	- Doug



More information about the cfe-dev mailing list