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.
git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@227649
138bc75d-0d04-0410-961f-
82ee72b054a4
+2015-09-09 Mark Wielaard <mjw@redhat.com>
+
+ * doc/invoke.texi (Wnonnull): Also warns when comparing against NULL.
+
2015-09-10 Oleg Endo <olegendo@gcc.gnu.org>
PR target/67506
+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
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)))
{
}
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)))
{
+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-10 Jakub Jelinek <jakub@redhat.com>
PR c++/67522
|| (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);
|| (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);
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.
+2015-09-09 Mark Wielaard <mjw@redhat.com>
+
+ * c-c++-common/nonnull-1.c: New test.
+
2015-09-10 Paul Thomas <pault@gcc.gnu.org>
PR fortran/66993
--- /dev/null
+/* 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 "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" } */
+}