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

Benjamin Kramer benny.kra at googlemail.com
Tue Jan 3 06:47:15 PST 2012


On 03.01.2012, at 15:14, Ed Schouten 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? If not, would it be
> nice to gain such a feature?

I'm not aware of clang having this warning and I think it is very useful.

One complication is that the warning shouldn't fire for weak symbols,
those can be NULL and checking that is usually intentional.

- Ben

> 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




More information about the cfe-dev mailing list