With gcc linkers and C, is there any way of triggering a linker error if a pointer finally evaluates to NULL?

56 Views Asked by At

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!

1

There are 1 best solutions below

0
Employed Russian On

is there any way of triggering a linker error if a pointer finally evaluates to NULL?

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 NULL pointer, the answer is no.

Detecting whether a program may dereference a NULL pointer is equivalent to the halting problem, which is known to be undecidable.