From patchwork Wed Sep 9 22:33:07 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Mark Wielaard X-Patchwork-Id: 516052 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 5FADD140281 for ; Thu, 10 Sep 2015 08:33:24 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b=DmWiHcSh; dkim-atps=neutral DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :message-id:subject:from:to:cc:date:in-reply-to:references :content-type:mime-version; q=dns; s=default; b=muOIf2wC7N3XGxdt W3Hffvtgj99OgooyFBcWDTAhejn1219fbMm1Y2E7A9DEvz47WYphO8DCYyCf/o0A 0ecQfOVH8O+OTqM8ZZF9qkfw4VO7hvBhBdQjOqihOe9ebzuifuswZn/uL+z+cr2h eMovzgEuC/8jVTy9DKZH/DeYG7w= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :message-id:subject:from:to:cc:date:in-reply-to:references :content-type:mime-version; s=default; bh=D6ZdMyrJZ9m8lqn3J8vZPV IdKGM=; b=DmWiHcSh4IkArx40dviBIOvR6gQr6D7VmpT8NkRC9JH4Es4AEcp6+P QiNkhxmJ6D2U9eMyWZR5U8pDo9G7DvHxwOXeofrVHCH2N8Am8FRP+Ezr7elGamrM 8k7Y2Jlc0q+mzhAwxhpkzjrB3vWKcWlLYdVW5TBq9DolXGKp4bRVM= Received: (qmail 86410 invoked by alias); 9 Sep 2015 22:33:13 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 86400 invoked by uid 89); 9 Sep 2015 22:33:12 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.7 required=5.0 tests=AWL, BAYES_00, KAM_LAZY_DOMAIN_SECURITY, SPF_HELO_PASS, T_RP_MATCHES_RCVD autolearn=no version=3.3.2 X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-GCM-SHA384 encrypted) ESMTPS; Wed, 09 Sep 2015 22:33:10 +0000 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) by mx1.redhat.com (Postfix) with ESMTPS id AEA538B136 for ; Wed, 9 Sep 2015 22:33:09 +0000 (UTC) Received: from bordewijk.wildebeest.org (ovpn-116-107.ams2.redhat.com [10.36.116.107]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id t89MX8mb015211 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Wed, 9 Sep 2015 18:33:09 -0400 Received: by bordewijk.wildebeest.org (Postfix, from userid 1000) id DA2788087810; Thu, 10 Sep 2015 00:33:07 +0200 (CEST) Message-ID: <1441837987.8165.127.camel@bordewijk.wildebeest.org> Subject: Re: [PATCH] Warn when comparing nonnull arguments to NULL in a function. From: Mark Wielaard To: Jakub Jelinek Cc: Jeff Law , gcc-patches@gcc.gnu.org Date: Thu, 10 Sep 2015 00:33:07 +0200 In-Reply-To: <20150909220328.GN1847@tucnak.redhat.com> References: <1441835087-14555-1-git-send-email-mjw@redhat.com> <55F0AC23.2010905@redhat.com> <20150909220328.GN1847@tucnak.redhat.com> Mime-Version: 1.0 On Thu, 2015-09-10 at 00:03 +0200, Jakub Jelinek wrote: > On Wed, Sep 09, 2015 at 04:01:07PM -0600, Jeff Law wrote: > > > * gcc.dg/nonnull-4.c: New test. > > > * g++.dg/warn/nonnull3.C: Likewise. > > If the tests are the same, perhaps stick just one test into > c-c++-common/nonnull-1.c instead? Yes, that would be better. The warnings should be exactly the same. > Also, all the "cp1 compared to NULL" > strings mention cp1, did you mean the second one to mention cp2 and so on? Oops. copy/paste error indeed. > > 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. Documentation added. bootstrap/regression test still running. Updated patch attached. Thanks, Mark From d8d71393c2fde83769d00c2da766a2fa7955ecbb Mon Sep 17 00:00:00 2001 From: Mark Wielaard Date: Wed, 9 Sep 2015 23:26:54 +0200 Subject: [PATCH] Warn when comparing nonnull arguments to NULL in a function. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- 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 618bbe6..86038f5 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-09 Kyrylo Tkachov * config/arm/arm.md (*subsi3_compare0): Rename to... 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 + + * c-typeck.c (build_binary_op): Check and warn when nonnull arg + parm against NULL. + 2015-09-09 Jakub Jelinek 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 + + * typeck.c (cp_build_binary_op): Check and warn when nonnull arg + parm against NULL. + 2015-09-09 Jakub Jelinek 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/doc/invoke.texi b/gcc/doc/invoke.texi index 76e5e29..dc171ec 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 360fe70..345caee 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-09 Kyrylo Tkachov * gcc.target/aarch64/mod_2.x: New file. 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 0000000..744c45f --- /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 "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" "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.4.3