[cfe-dev] Warning when comparing address of function or variable with constant?
Jean-Daniel Dupas
devlists at shadowlab.org
Tue Jan 3 06:59:10 PST 2012
Le 3 janv. 2012 à 15:14, Ed Schouten a écrit :
> 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? If not, would it be
> nice to gain such a feature?
Note that a function can be NULL if it is declared weak on Darwin, which is something pretty common as it is used for backward compatibility.
> GCC (4.2) seems to support something like this, but doesn't do it
> properly. The following code triggers a warning:
>
> if (index != 0)
> puts("Hi");
>
> While this code does not:
>
> if (index != NULL)
> puts("Hi");
>
> Essentially the compiler would be free to emit a warning for comparing
> an address of a function or variable with any constant expression. As
> things like address space randomisation become more prevalent, a fixed
> address means nothing.
>
> --
> Ed Schouten <ed at 80386.nl>
> WWW: http://80386.nl/
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
-- Jean-Daniel
More information about the cfe-dev
mailing list