gcc warn when pointers not checked non-null before de-referencing.

Martin Sebor msebor@gmail.com
Mon Jun 14 15:19:09 GMT 2021

On 6/13/21 4:30 PM, Jonny Grant wrote:
> Hello
> This isn't real code, just an example to show.
> I've tried with:  -Wall -Wextra -O2  and some other warnings, but couldn't get this to generate a warning that *g was possibly de-referenced. May I ask, does GCC have a way to get warnings when pointers are not checked?
> I had a look but -Wnull-dereference didn't help.

The pointer test is eliminated before -Wnull-dereference runs so it
can't do anything in this case.  -fno-delete-null-pointer-checks
disables the optimization and retains the test so -Wnull-dereference
could in theory detect it but it isn't designed to do it.  But if
it did, I suspect it would be quite noisy as a result of other
transformations like inlining and constant propagation.  So making
it work with an acceptable S/R ratio would take more work than just
enhancing -Wnull-dereference.


> #include <stdlib.h>
> #include <cstddef>
> void f(int * g)
> {
>      *g = 1;
>      if(NULL == g)
>      {
>          exit(1);
>      }
> }
> Best regards Jonny

More information about the Gcc-help mailing list