From patchwork Tue Oct 31 19:05:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Martin Uecker X-Patchwork-Id: 160311 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b90f:0:b0:403:3b70:6f57 with SMTP id t15csp454103vqg; Tue, 31 Oct 2023 12:05:46 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHHxoWAKGAvnmDc4a0jHySNDc2oBicoTA5AIggKLCBkirpwNDlbLd6dKfJuiHkONjG+Z31f X-Received: by 2002:a05:620a:4621:b0:778:91f0:e541 with SMTP id br33-20020a05620a462100b0077891f0e541mr16079399qkb.23.1698779145874; Tue, 31 Oct 2023 12:05:45 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1698779145; cv=pass; d=google.com; s=arc-20160816; b=iG8s4Gy7Spvg1mcfGvJ4XXI90HMAvBUV8Y14D9ziXb8MY8zascEMO1BrV9SHN69sx4 +ml/sl8DSDmriUOdC346rnJkKDKPwrNhdiIPsSUO3ResHXX1j5CfZb6yGdq3NfjsOCg/ iRNfWAj/Ot9xJgIxb9IfiONtnrOQH6djCorM7G8LcNSp681wo4ZxcfjZrZA+LZtrVm7j tmTA7BoGUe0m4y8FxRuhMqCcDtRiy7xIGsk25MjJKBMzq+pl6F6EzxhQCuyLT7jAH3H8 KTm9zF+mMT7QUreV/Kybm+aVvyHhAKz5rqhBGCSDunsPGQlSeWvF9WTmJmy2JDke24qu Qn/A== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:mime-version:user-agent :content-transfer-encoding:date:cc:to:from:subject:message-id :dkim-signature:dkim-filter:arc-filter:dmarc-filter:delivered-to; bh=7S3fFh0Xxa0utX6v9qWOJai51LaCxUMo4t3MwsJetyI=; fh=UnJaSs1LdmHl1brBz3CJXOVVEZ0KHynh+GYvTrOkcOg=; b=V+yPecmM3EXfpT8dkYM/gkPc4v6bqd9YM1H+tHIVHFPSECVV7DvFRZv3DJvegKjwQb n8GwBpWK2tcB1kePUPF+TStAYL/FvGJBjdD6hR4c7MXIIZzXj79Up7JQZAfbIyEtf4ol BUr5GzB8JSHXrNkku7mQkuy02NMLlneVdPS3hlTVDe0Hs7XgLA2l95w2J5d0HaAga1Hx olc6sFtGqOSoymiIpMjc7WM8kXNEiKAK8Kdvx7YyYoGOl+ebjJZnhhSKgRQQkUQPX9UK HX0+PTFND9ijvskxm5duO0Fsd2eQTncxu4PnlMyH5ZFvQnem//HxvZ312Cgvm3rTiSld xDaA== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@tugraz.at header.s=mailrelay header.b=Su3MeP1Q; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=QUARANTINE sp=NONE dis=NONE) header.from=tugraz.at Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id k11-20020a05620a07eb00b00775b023f298si1582730qkk.139.2023.10.31.12.05.45 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 31 Oct 2023 12:05:45 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@tugraz.at header.s=mailrelay header.b=Su3MeP1Q; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=QUARANTINE sp=NONE dis=NONE) header.from=tugraz.at Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 9A7BC3857C44 for ; Tue, 31 Oct 2023 19:05:45 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mailrelay.tugraz.at (mailrelay.tugraz.at [129.27.2.202]) by sourceware.org (Postfix) with ESMTPS id BB44E3858D1E for ; Tue, 31 Oct 2023 19:05:18 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org BB44E3858D1E Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=tugraz.at Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=tugraz.at ARC-Filter: OpenARC Filter v1.0.0 sourceware.org BB44E3858D1E Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=129.27.2.202 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698779120; cv=none; b=MRYe+UCRSC2v+MTr/njPN1STLSCGXb0b4wD5SWPrVpiAsmh1rQZnflncGKCHwJEtuXe1oUlBqYOqNRzmEuYDYt6+Qyh8useHqkZpAXrjhcjgH3vSQF1Anf6/eD5x4i1JTWBM9f1Yl8eJYiKetvV/jGQOGic083IDJ6urdp7bQJU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698779120; c=relaxed/simple; bh=fnMGD8Pi82QJmLbzvRoCk5B2mPrv9gMxAASGttnhob4=; h=DKIM-Signature:Message-ID:Subject:From:To:Date:MIME-Version; b=U5+aa+gOTIpY3WrOM4d0BLTbgY0FERlosIHRQ4x41h8mxtPxkoKy4HHn4LAGrzG7AYMwcLUAocIRDvXYNKGKoUlJQ31PY3KatXTI5I/v8dzo74kdq95HhXtCoog5XWrybIw4phWCiuJfLuU5pEbB3LAYjmKDJWKOLnx7KPVz2XQ= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from vra-169-132.tugraz.at (vra-169-132.tugraz.at [129.27.169.132]) by mailrelay.tugraz.at (Postfix) with ESMTPSA id 4SKfj16FKNz1LM0R; Tue, 31 Oct 2023 20:05:09 +0100 (CET) DKIM-Filter: OpenDKIM Filter v2.11.0 mailrelay.tugraz.at 4SKfj16FKNz1LM0R DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tugraz.at; s=mailrelay; t=1698779109; bh=7S3fFh0Xxa0utX6v9qWOJai51LaCxUMo4t3MwsJetyI=; h=Subject:From:To:Cc:Date:From; b=Su3MeP1QFCY0GsyGcLnz3mfuZIDPN1bjeAjwmwD51JorxZpFYNo6/iEJEHXfB7e2v KF7Uwd0L594uiMgHyGmyi6laphwnFpOsA0GK0AS0E06cY+kwecDbUHP5Rnn4hiOXdk V3eFGfxdMl7Bw8ZwGwFH5tqtg84Z3TzegF4qG1rk= Message-ID: Subject: [PATCH] Reduce false positives for -Wnonnull for VLA parameters [PR98541] From: Martin Uecker To: gcc-patches@gcc.gnu.org Cc: Richard Biener Date: Tue, 31 Oct 2023 20:05:09 +0100 User-Agent: Evolution 3.46.4-2 MIME-Version: 1.0 X-TUG-Backscatter-control: G/VXY7/6zeyuAY/PU2/0qw X-Spam-Scanner: SpamAssassin 3.003001 X-Spam-Score-relay: -1.9 X-Scanned-By: MIMEDefang 2.74 on 129.27.10.116 X-Spam-Status: No, score=-11.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1781299041598690902 X-GMAIL-MSGID: 1781299041598690902 This is a revised part of previously posted patch which I split up. C FE changes which another false positive were already merged, but I still need approval for this middle-end change. It would be nice to get this in, because it fixes some rather annoying (for me atleast) false positive warnings with no easy workaround. In the following example, int foo(int n, float matrix[n], float opt[n]); foo(n, matrix, NULL); GCC warns about NULL iff n > 0. This is problematic for several reasons: 1. It causes false positives (and I turn off -Wnonnull in one of my projects for this reason) 2. It is inconsistent with regular arrays where there is no warning in this case. 3. The size parameter is sometimes shared (as in this example) so passing zero to avoid the warning is only possible by making the code more complex. 4. Passing zero as a workaround is technically UB. (The original author of the warning code, Martin S seemed to  agree with this change according to this discussion in Bugzilla.) Reduce false positives for -Wnonnull for VLA parameters [PR98541] This patch limits the warning about NULL arguments to VLA parameters declared [static n]. PR c/98541 gcc/ * gimple-ssa-warn-access.cc (pass_waccess::maybe_check_access_sizes): For VLA bounds in parameters, only warn about null pointers with 'static'. gcc/testsuite: * gcc.dg/Wnonnull-4: Adapt test. * gcc.dg/Wstringop-overflow-40.c: Adapt test. diff --git a/gcc/gimple-ssa-warn-access.cc b/gcc/gimple-ssa-warn-access.cc index e439d1b9b68..8b734295f09 100644 --- a/gcc/gimple-ssa-warn-access.cc +++ b/gcc/gimple-ssa-warn-access.cc @@ -3477,27 +3477,14 @@ pass_waccess::maybe_check_access_sizes (rdwr_map *rwm, tree fndecl, tree fntype, if (integer_zerop (ptr)) { - if (sizidx >= 0 && tree_int_cst_sgn (sizrng[0]) > 0) + if (!access.second.internal_p + && sizidx >= 0 && tree_int_cst_sgn (sizrng[0]) > 0) { /* Warn about null pointers with positive sizes. This is different from also declaring the pointer argument with attribute nonnull when the function accepts null pointers only when the corresponding size is zero. */ - if (access.second.internal_p) - { - const std::string argtypestr - = access.second.array_as_string (ptrtype); - - if (warning_at (loc, OPT_Wnonnull, - "argument %i of variable length " - "array %s is null but " - "the corresponding bound argument " - "%i value is %s", - ptridx + 1, argtypestr.c_str (), - sizidx + 1, sizstr)) - arg_warned = OPT_Wnonnull; - } - else if (warning_at (loc, OPT_Wnonnull, + if (warning_at (loc, OPT_Wnonnull, "argument %i is null but " "the corresponding size argument " "%i value is %s", diff --git a/gcc/testsuite/gcc.dg/Wnonnull-4.c b/gcc/testsuite/gcc.dg/Wnonnull-4.c index 2c1c45a9856..1f14fbba45d 100644 --- a/gcc/testsuite/gcc.dg/Wnonnull-4.c +++ b/gcc/testsuite/gcc.dg/Wnonnull-4.c @@ -27,9 +27,9 @@ void test_fca_n (int r_m1) T ( 0); // Verify positive bounds. - T ( 1); // { dg-warning "argument 2 of variable length array 'char\\\[n]' is null but the corresponding bound argument 1 value is 1" } - T ( 9); // { dg-warning "argument 2 of variable length array 'char\\\[n]' is null but the corresponding bound argument 1 value is 9" } - T (max); // { dg-warning "argument 2 of variable length array 'char\\\[n]' is null but the corresponding bound argument 1 value is \\d+" } + T ( 1); // { dg-bogus "argument 2 of variable length array 'char\\\[n]' is null but the corresponding bound argument 1 value is 1" } + T ( 9); // { dg-bogus "argument 2 of variable length array 'char\\\[n]' is null but the corresponding bound argument 1 value is 9" } + T (max); // { dg-bogus "argument 2 of variable length array 'char\\\[n]' is null but the corresponding bound argument 1 value is \\d+" } } @@ -55,9 +55,9 @@ void test_fsa_x_n (int r_m1) T ( 0); // Verify positive bounds. - T ( 1); // { dg-warning "argument 2 of variable length array 'short int\\\[]\\\[n]' is null but the corresponding bound argument 1 value is 1" } - T ( 9); // { dg-warning "argument 2 of variable length array 'short int\\\[]\\\[n]' is null but the corresponding bound argument 1 value is 9" } - T (max); // { dg-warning "argument 2 of variable length array 'short int\\\[]\\\[n]' is null but the corresponding bound argument 1 value is \\d+" } + T ( 1); // { dg-bogus "argument 2 of variable length array 'short int\\\[]\\\[n]' is null but the corresponding bound argument 1 value is 1" } + T ( 9); // { dg-bogus "argument 2 of variable length array 'short int\\\[]\\\[n]' is null but the corresponding bound argument 1 value is 9" } + T (max); // { dg-bogus "argument 2 of variable length array 'short int\\\[]\\\[n]' is null but the corresponding bound argument 1 value is \\d+" } } @@ -83,9 +83,9 @@ void test_fia_1_n (int r_m1) T ( 0); // Verify positive bounds. - T ( 1); // { dg-warning "argument 2 of variable length array 'int\\\[1]\\\[n]' is null but the corresponding bound argument 1 value is 1" } - T ( 9); // { dg-warning "argument 2 of variable length array 'int\\\[1]\\\[n]' is null but the corresponding bound argument 1 value is 9" } - T (max); // { dg-warning "argument 2 of variable length array 'int\\\[1]\\\[n]' is null but the corresponding bound argument 1 value is \\d+" } + T ( 1); // { dg-bogus "argument 2 of variable length array 'int\\\[1]\\\[n]' is null but the corresponding bound argument 1 value is 1" } + T ( 9); // { dg-bogus "argument 2 of variable length array 'int\\\[1]\\\[n]' is null but the corresponding bound argument 1 value is 9" } + T (max); // { dg-bogus "argument 2 of variable length array 'int\\\[1]\\\[n]' is null but the corresponding bound argument 1 value is \\d+" } } @@ -111,9 +111,9 @@ void test_fla_3_n (int r_m1) T ( 0); // Verify positive bounds. - T ( 1); // { dg-warning "argument 2 of variable length array 'long int\\\[3]\\\[n]' is null but the corresponding bound argument 1 value is 1" } - T ( 9); // { dg-warning "argument 2 of variable length array 'long int\\\[3]\\\[n]' is null but the corresponding bound argument 1 value is 9" } - T (max); // { dg-warning "argument 2 of variable length array 'long int\\\[3]\\\[n]' is null but the corresponding bound argument 1 value is \\d+" } + T ( 1); // { dg-bogus "argument 2 of variable length array 'long int\\\[3]\\\[n]' is null but the corresponding bound argument 1 value is 1" } + T ( 9); // { dg-bogus "argument 2 of variable length array 'long int\\\[3]\\\[n]' is null but the corresponding bound argument 1 value is 9" } + T (max); // { dg-bogus "argument 2 of variable length array 'long int\\\[3]\\\[n]' is null but the corresponding bound argument 1 value is \\d+" } } @@ -139,9 +139,9 @@ void test_fda_n_5 (int r_m1) T ( 0); // Verify positive bounds. - T ( 1); // { dg-warning "argument 2 of variable length array 'double\\\[n]\\\[5]' is null but the corresponding bound argument 1 value is 1" } - T ( 9); // { dg-warning "argument 2 of variable length array 'double\\\[n]\\\[5]' is null but the corresponding bound argument 1 value is 9" } - T (max); // { dg-warning "argument 2 of variable length array 'double\\\[n]\\\[5]' is null but the corresponding bound argument 1 value is \\d+" } + T ( 1); // { dg-bogus "argument 2 of variable length array 'double\\\[n]\\\[5]' is null but the corresponding bound argument 1 value is 1" } + T ( 9); // { dg-bogus "argument 2 of variable length array 'double\\\[n]\\\[5]' is null but the corresponding bound argument 1 value is 9" } + T (max); // { dg-bogus "argument 2 of variable length array 'double\\\[n]\\\[5]' is null but the corresponding bound argument 1 value is \\d+" } } @@ -167,7 +167,7 @@ void test_fca_n_n (int r_m1) T ( 0); // Verify positive bounds. - T ( 1); // { dg-warning "argument 2 of variable length array 'char\\\[n]\\\[n]' is null but the corresponding bound argument 1 value is 1" } - T ( 9); // { dg-warning "argument 2 of variable length array 'char\\\[n]\\\[n]' is null but the corresponding bound argument 1 value is 9" } - T (max); // { dg-warning "argument 2 of variable length array 'char\\\[n]\\\[n]' is null but the corresponding bound argument 1 value is \\d+" } + T ( 1); // { dg-bogus "argument 2 of variable length array 'char\\\[n]\\\[n]' is null but the corresponding bound argument 1 value is 1" } + T ( 9); // { dg-bogus "argument 2 of variable length array 'char\\\[n]\\\[n]' is null but the corresponding bound argument 1 value is 9" } + T (max); // { dg-bogus "argument 2 of variable length array 'char\\\[n]\\\[n]' is null but the corresponding bound argument 1 value is \\d+" } } diff --git a/gcc/testsuite/gcc.dg/Wstringop-overflow-40.c b/gcc/testsuite/gcc.dg/Wstringop-overflow-40.c index 386c92dc7a8..9e0ad1f3aff 100644 --- a/gcc/testsuite/gcc.dg/Wstringop-overflow-40.c +++ b/gcc/testsuite/gcc.dg/Wstringop-overflow-40.c @@ -11,6 +11,7 @@ void fxa2 (int16_t[2]) __attribute__ ((nonnull)); void fas2 (int16_t[static 2]); void fvla (unsigned n, int16_t[n]); +void fvlaS (unsigned n, int16_t[static n]); void test_array_1_dim (void) { @@ -33,7 +34,8 @@ void test_array_1_dim (void) fas2 (a1); // { dg-warning "'fas2' accessing 4 bytes in a region of size 2 " } fas2 (&i); // { dg-warning "'fas2' accessing 4 bytes in a region of size 2 " } - fvla (1, 0); // { dg-warning "\\\[-Wnonnull" } + fvla (1, 0); + fvlaS (1, 0); // { dg-warning "\\\[-Wnonnull" } fvla (1, &i); fvla (2, a2); fvla (2, a1); // { dg-warning "'fvla' accessing 4 bytes in a region of size 2 " } @@ -47,6 +49,7 @@ void fxac2 (const int16_t[2]) __attribute__ ((nonnull)); void facs2 (const int16_t[static 2]); void fvlac (unsigned n, const int16_t[n]); +void fvlacS (unsigned n, const int16_t[static n]); void test_const_array_1_dim (void) { @@ -69,7 +72,8 @@ void test_const_array_1_dim (void) facs2 (a1); // { dg-warning "'facs2' reading 4 bytes from a region of size 2 " } facs2 (&i); // { dg-warning "'facs2' reading 4 bytes from a region of size 2 " } - fvlac (1, 0); // { dg-warning "\\\[-Wnonnull" } + fvlac (1, 0); + fvlacS (1, 0); // { dg-warning "\\\[-Wnonnull" } fvlac (1, &i); fvlac (2, a2); fvlac (2, a1); // { dg-warning "'fvlac' reading 4 bytes from a region of size 2 " }