From: Mark Wielaard <mjw@redhat.com>
To: gcc-patches@gcc.gnu.org
Cc: Mark Wielaard <mjw@redhat.com>
Subject: [PATCH] Warn when comparing nonnull arguments to NULL in a function.
Date: Wed, 09 Sep 2015 21:51:00 -0000 [thread overview]
Message-ID: <1441835087-14555-1-git-send-email-mjw@redhat.com> (raw)
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.
---
gcc/c/ChangeLog | 5 +++++
gcc/c/c-typeck.c | 10 ++++++++++
gcc/cp/ChangeLog | 5 +++++
gcc/cp/typeck.c | 10 ++++++++++
gcc/testsuite/ChangeLog | 5 +++++
gcc/testsuite/g++.dg/warn/nonnull3.C | 29 +++++++++++++++++++++++++++++
gcc/testsuite/gcc.dg/nonnull-4.c | 28 ++++++++++++++++++++++++++++
7 files changed, 92 insertions(+)
create mode 100644 gcc/testsuite/g++.dg/warn/nonnull3.C
create mode 100644 gcc/testsuite/gcc.dg/nonnull-4.c
diff --git a/gcc/c/ChangeLog b/gcc/c/ChangeLog
index d7eeb2d..35ccdda 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-09 Jakub Jelinek <jakub@redhat.com>
PR c/67501
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)))
{
diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog
index 515a1e8..7cf0064 100644
--- a/gcc/cp/ChangeLog
+++ b/gcc/cp/ChangeLog
@@ -1,3 +1,8 @@
+2015-09-09 Mark Wielaard <mjw@redhat.com>
+
+ * typeck.c (cp_build_binary_op): Check and warn when nonnull arg
+ parm against NULL.
+
2015-09-09 Jakub Jelinek <jakub@redhat.com>
PR c++/67504
diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c
index 388558c..482e42c 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/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index 360fe70..be4abd0 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,8 @@
+2015-09-09 Mark Wielaard <mjw@redhat.com>
+
+ * gcc.dg/nonnull-4.c: New test.
+ * g++.dg/warn/nonnull3.C: Likewise.
+
2015-09-09 Kyrylo Tkachov <kyrylo.tkachov@arm.com>
* gcc.target/aarch64/mod_2.x: New file.
diff --git a/gcc/testsuite/g++.dg/warn/nonnull3.C b/gcc/testsuite/g++.dg/warn/nonnull3.C
new file mode 100644
index 0000000..8cad937
--- /dev/null
+++ b/gcc/testsuite/g++.dg/warn/nonnull3.C
@@ -0,0 +1,29 @@
+/* Test for the bad usage of "nonnull" function attribute parms. */
+/* Same as C test gcc.dg/nonnull-4.c because checks are done in frontend. */
+/* */
+/* { dg-do compile } */
+/* { dg-options "-Wnonnull" } */
+
+#include <stddef.h>
+#include <stdlib.h>
+
+void foo(void *bar) __attribute__((nonnull(1)));
+
+void foo(void *bar) { if (!bar) abort(); } /* { dg-warning "null" "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" "cp1 compared to NULL" } */
+ return 2;
+
+ if (NULL != cp3) /* { dg-warning "nonnull argument" "cp1 compared to NULL" } */
+ return 3;
+
+ return (cp4 != 0) ? 0 : 1; /* { dg-warning "nonnull argument" "cp1 compared to NULL" } */
+}
diff --git a/gcc/testsuite/gcc.dg/nonnull-4.c b/gcc/testsuite/gcc.dg/nonnull-4.c
new file mode 100644
index 0000000..12f9356
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/nonnull-4.c
@@ -0,0 +1,28 @@
+/* Test for the bad usage of "nonnull" function attribute parms. */
+/* */
+/* { dg-do compile } */
+/* { dg-options "-Wnonnull" } */
+
+#include <stddef.h>
+#include <stdlib.h>
+
+void foo(void *bar) __attribute__((nonnull(1)));
+
+void foo(void *bar) { if (!bar) abort(); } /* { dg-warning "null" "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" "cp1 compared to NULL" } */
+ return 2;
+
+ if (NULL != cp3) /* { dg-warning "nonnull argument" "cp1 compared to NULL" } */
+ return 3;
+
+ return (cp4 != 0) ? 0 : 1; /* { dg-warning "nonnull argument" "cp1 compared to NULL" } */
+}
--
2.4.3
next reply other threads:[~2015-09-09 21:45 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-09-09 21:51 Mark Wielaard [this message]
2015-09-09 22:02 ` Jeff Law
2015-09-09 22:33 ` Jakub Jelinek
2015-09-09 23:01 ` Mark Wielaard
2015-09-14 19:26 ` Jeff Law
2015-09-15 3:43 ` Martin Sebor
2015-09-15 8:48 ` Mark Wielaard
2015-09-15 12:22 ` Manuel López-Ibáñez
2015-09-15 16:27 ` Mark Wielaard
2015-09-15 14:56 ` Martin Sebor
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1441835087-14555-1-git-send-email-mjw@redhat.com \
--to=mjw@redhat.com \
--cc=gcc-patches@gcc.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).