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

Ed Schouten ed at 80386.nl
Tue Jan 3 06:14:10 PST 2012

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?

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/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 834 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20120103/5f0ed408/attachment.sig>

More information about the cfe-dev mailing list