From dbb68221b64c0174eeb22d878a8e078775ee73bf Mon Sep 17 00:00:00 2001 From: Mark Wielaard Date: Thu, 10 Sep 2015 15:29:44 +0000 Subject: [PATCH] Warn when comparing nonnull arguments to NULL in a function. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 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 --- gcc/ChangeLog | 4 ++++ gcc/c/ChangeLog | 5 +++++ gcc/c/c-typeck.c | 10 +++++++++ gcc/cp/ChangeLog | 5 +++++ gcc/cp/typeck.c | 10 +++++++++ gcc/doc/invoke.texi | 3 +++ gcc/testsuite/ChangeLog | 4 ++++ gcc/testsuite/c-c++-common/nonnull-1.c | 28 ++++++++++++++++++++++++++ 8 files changed, 69 insertions(+) create mode 100644 gcc/testsuite/c-c++-common/nonnull-1.c diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 67367eb8939..df8cc5d6aee 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,7 @@ +2015-09-09 Mark Wielaard + + * doc/invoke.texi (Wnonnull): Also warns when comparing against NULL. + 2015-09-10 Oleg Endo PR target/67506 diff --git a/gcc/c/ChangeLog b/gcc/c/ChangeLog index cfc20aa3b5f..325686a4b1c 100644 --- a/gcc/c/ChangeLog +++ b/gcc/c/ChangeLog @@ -1,3 +1,8 @@ +2015-09-09 Mark Wielaard + + * c-typeck.c (build_binary_op): Check and warn when nonnull arg + parm against NULL. + 2015-09-10 Jakub Jelinek PR c/67502 diff --git a/gcc/c/c-typeck.c b/gcc/c/c-typeck.c index dc223969b3e..4108f27ab7c 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))) { diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 32646e0a1ba..339277339b7 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,8 @@ +2015-09-09 Mark Wielaard + + * typeck.c (cp_build_binary_op): Check and warn when nonnull arg + parm against NULL. + 2015-09-10 Jakub Jelinek PR c++/67522 diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c index 388558c347d..482e42c819b 100644 --- a/gcc/cp/typeck.c +++ b/gcc/cp/typeck.c @@ -4438,6 +4438,11 @@ cp_build_binary_op (location_t location, || (code0 == POINTER_TYPE && TYPE_PTR_P (type1) && integer_zerop (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 (TYPE_PTR_P (type1)) result_type = composite_pointer_type (type0, type1, op0, op1, CPO_COMPARISON, complain); @@ -4477,6 +4482,11 @@ cp_build_binary_op (location_t location, || (code1 == POINTER_TYPE && TYPE_PTR_P (type0) && integer_zerop (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 (TYPE_PTR_P (type0)) result_type = composite_pointer_type (type0, type1, op0, op1, CPO_COMPARISON, complain); diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index 403cebe2c15..518d68933c9 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -3720,6 +3720,9 @@ formats that may yield only a two-digit year. Warn about passing a null pointer for arguments marked as requiring a non-null value by the @code{nonnull} function attribute. +Also warns when comparing an argument marked with the @code{nonnull} +function attribute against null inside the function. + @option{-Wnonnull} is included in @option{-Wall} and @option{-Wformat}. It can be disabled with the @option{-Wno-nonnull} option. diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 1671b4c11ff..f99682cf352 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,7 @@ +2015-09-09 Mark Wielaard + + * c-c++-common/nonnull-1.c: New test. + 2015-09-10 Paul Thomas PR fortran/66993 diff --git a/gcc/testsuite/c-c++-common/nonnull-1.c b/gcc/testsuite/c-c++-common/nonnull-1.c new file mode 100644 index 00000000000..b5c3d7f8866 --- /dev/null +++ b/gcc/testsuite/c-c++-common/nonnull-1.c @@ -0,0 +1,28 @@ +/* Test for the bad usage of "nonnull" function attribute parms. */ +/* */ +/* { dg-do compile } */ +/* { dg-options "-Wnonnull" } */ + +#include +#include + +void foo(void *bar) __attribute__((nonnull(1))); + +void foo(void *bar) { if (!bar) abort(); } /* { dg-warning "nonnull argument" "bar compared to NULL" } */ + +extern int func (char *, char *, char *, char *) __attribute__((nonnull)); + +int +func (char *cp1, char *cp2, char *cp3, char *cp4) +{ + if (cp1) /* { dg-warning "nonnull argument" "cp1 compared to NULL" } */ + return 1; + + if (cp2 == NULL) /* { dg-warning "nonnull argument" "cp2 compared to NULL" } */ + return 2; + + if (NULL != cp3) /* { dg-warning "nonnull argument" "cp3 compared to NULL" } */ + return 3; + + return (cp4 != 0) ? 0 : 1; /* { dg-warning "nonnull argument" "cp4 compared to NULL" } */ +} -- 2.30.2