[cfe-dev] Warning when comparing address of function or variable with constant?
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
> func(struct foo *idx)
> if (index == NULL)
> 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)
> While this code does not:
> if (index != NULL)
> 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
More information about the cfe-dev