[PATCH] Warn when comparing nonnull arguments to NULL in a function.

Jeff Law law@redhat.com
Wed Sep 9 22:02:00 GMT 2015


On 09/09/2015 03:44 PM, Mark Wielaard wrote:
> The following found 14 bugs in my code base. I think it is useful to
> warn about such usage since they are bugsr. If the argument is marked
> as nonnull then passing in a NULL argument will produce bad results
> even if the code checks against NULL.
>
> GCC might optimize such checks away so warn the user when the function
> contains such comparisions.
>
> nn.c: In function ‘foo’:
> nn.c:6:27: warning: nonnull argument ‘bar’ compared to NULL [-Wnonnull]
>   void foo(void *bar) { if (!bar) abort(); }
>                             ^
> gcc/c/ChangeLog
>
>         * c-typeck.c (build_binary_op): Check and warn when nonnull arg
>         parm against NULL.
>
> gcc/cp/ChangeLog
>
>         * typeck.c (cp_build_binary_op): Check and warn when nonnull arg
>         parm against NULL.
>
> gcc/testsuite/ChangeLog
>
>         * gcc.dg/nonnull-4.c: New test.
>         * g++.dg/warn/nonnull3.C: Likewise.
Can you also upate the -Wnonnull documentation in invoke.texi to 
indicate it also will warn if it discovers a non-null argument that is 
compared against null?

With the doc fix and a bootstrap/regression test, this patch ought to be 
fine.

jeff



More information about the Gcc-patches mailing list