diff options
author | Mark Wielaard <mjw@redhat.com> | 2015-09-10 15:29:44 +0000 |
---|---|---|
committer | Mark Wielaard <mark@gcc.gnu.org> | 2015-09-10 15:29:44 +0000 |
commit | dbb68221b64c0174eeb22d878a8e078775ee73bf (patch) | |
tree | f34f064a585942929fce66827ae853b1f48d5357 /gcc/c | |
parent | 0815fd250302b45a3aadd9b32d8559c7b47d86e4 (diff) | |
download | gcc-dbb68221b64c0174eeb22d878a8e078775ee73bf.zip gcc-dbb68221b64c0174eeb22d878a8e078775ee73bf.tar.gz gcc-dbb68221b64c0174eeb22d878a8e078775ee73bf.tar.bz2 |
Warn when comparing nonnull arguments to NULL in a function.
If an 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 comparisons.
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/ChangeLog
* doc/invoke.texi (Wnonnull): Also warns when comparing against NULL.
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
* c-c++-common/nonnull-1.c: New test.
From-SVN: r227649
Diffstat (limited to 'gcc/c')
-rw-r--r-- | gcc/c/ChangeLog | 5 | ||||
-rw-r--r-- | gcc/c/c-typeck.c | 10 |
2 files changed, 15 insertions, 0 deletions
diff --git a/gcc/c/ChangeLog b/gcc/c/ChangeLog index cfc20aa..325686a 100644 --- a/gcc/c/ChangeLog +++ b/gcc/c/ChangeLog @@ -1,3 +1,8 @@ +2015-09-09 Mark Wielaard <mjw@redhat.com> + + * c-typeck.c (build_binary_op): Check and warn when nonnull arg + parm against NULL. + 2015-09-10 Jakub Jelinek <jakub@redhat.com> PR c/67502 diff --git a/gcc/c/c-typeck.c b/gcc/c/c-typeck.c index dc22396..4108f27 100644 --- a/gcc/c/c-typeck.c +++ b/gcc/c/c-typeck.c @@ -10803,6 +10803,11 @@ build_binary_op (location_t location, enum tree_code code, short_compare = 1; else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1)) { + if (warn_nonnull + && TREE_CODE (op0) == PARM_DECL && nonnull_arg_p (op0)) + warning_at (location, OPT_Wnonnull, + "nonnull argument %qD compared to NULL", op0); + if (TREE_CODE (op0) == ADDR_EXPR && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0))) { @@ -10823,6 +10828,11 @@ build_binary_op (location_t location, enum tree_code code, } else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0)) { + if (warn_nonnull + && TREE_CODE (op1) == PARM_DECL && nonnull_arg_p (op1)) + warning_at (location, OPT_Wnonnull, + "nonnull argument %qD compared to NULL", op1); + if (TREE_CODE (op1) == ADDR_EXPR && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0))) { |