At first, I tried using __attribute__ ((__nonnull__)) to do this, but this only triggers a gcc compiler warning when the parameter expression was immediately NULL. The problem for me is that if the parameter expression instead subsequently evaluates to NULL inside the linker (e.g. if it was indirected via a const pointer dereference), this nonnull test doesn't appear to get triggered.
So, is there any way of triggering some kind of gcc linker error when an expression finally evaluates to 0 inside the linker? I would like to use this as a kind of really late static assert, because nonnull doesn't seem late enough to be genuinely safe.
Note: I'm only looking for a gcc-specific linker trick for this at the moment, I'm not interested in other compilers/linkers. Thanks!
Assuming that you mean: is there a way to make a program fail to build (at compile or link time) if the program might at execution time try to dereference a
NULLpointer, the answer is no.Detecting whether a program may dereference a
NULLpointer is equivalent to the halting problem, which is known to be undecidable.