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

Matt Beaumont-Gay matthewbg at google.com
Tue Jan 3 08:20:42 PST 2012


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.

Cheers,
Matt




More information about the cfe-dev mailing list