From patchwork Wed Mar 29 12:11:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 76555 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp364245vqo; Wed, 29 Mar 2023 05:12:42 -0700 (PDT) X-Google-Smtp-Source: AKy350YT69Kh3PKZsasovxEFe1jf0v5C1Gg1sErPoFLKIdHjj52AWTz4Qfzs6OZAV4/9/gWZMDfO X-Received: by 2002:a17:906:4796:b0:93f:3084:d6f with SMTP id cw22-20020a170906479600b0093f30840d6fmr18970653ejc.18.1680091962507; Wed, 29 Mar 2023 05:12:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680091962; cv=none; d=google.com; s=arc-20160816; b=WSTwUpqW6A2oVsLK6NONuuwxQ4ZIvNkrjd48dvBJfFqVWkvXXk8zZhr4aULpb513DR S441QlbTggxVR+ahLAn7nQJdELHMvLKVNvJKwvJFq9cwYzOAzjIv5G7QwPNJXEd19IPD HAAi0KzBjSlrCDTBdriQdfOPpjXY0wFZgQ+APn88rQu/ObKYwyT1QrAbUGh+1AkyowK7 5+ww7emQzFpobRApinjIStPCr0ockZxHhlLFkVHR8tH+pSOGyd/n2+BO6Z8ACRL5Vvpj J+B0NnkF4s2TL+0NTGfmbSZTu0jabmTr+X3VbnWQYViJbL/HIGFL3sKQCtdOXSUrljt3 NlHQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=message-id:sender:errors-to:reply-to:from:list-subscribe:list-help :list-post:list-archive:list-unsubscribe:list-id:precedence :mime-version:user-agent:subject:cc:to:date:dmarc-filter :delivered-to:dkim-signature:dkim-filter; bh=/bOk8y8Af0n4kcg71XGBkj7JdtEpqcHsLcnCZqrNXWk=; b=wMJ1Zu2M6aeAeENSiuQPxoklld7BdHFWmEtsyrxd/M+WeIOmm84r5JW6vfvG7QetvR D/PidQFLBuEJ7+Wyk6tY68EpUotgB0+IZ7IDsSpJSvkfLmliu0fnCW8QCpD8ZrvSYg0r 43O7x52kAMWMOja6e2QG1uvM0RbjXvoYkH+eXQFtMyGx6QWXHZlFQxsMcOxj3scRy6O2 2E5Zr9pZvfbUXT5rE6xWWSMkqZtRjHNQK8IBphv94TiU5MZzFQkXoLscsB58CqSVclUe 2mHT2+iX/9GMEK9cWsMss/f+cOYTvluFjzdFw2vY43ELJ+4QnEoy5jh6C3eIZzzsbDok 07jA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=R3Ii5tsM; 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=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id a13-20020a1709066d4d00b0092be4d3413asi32251584ejt.131.2023.03.29.05.12.42 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 Mar 2023 05:12:42 -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=@gcc.gnu.org header.s=default header.b=R3Ii5tsM; 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=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B0266385B538 for ; Wed, 29 Mar 2023 12:12:33 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B0266385B538 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1680091953; bh=/bOk8y8Af0n4kcg71XGBkj7JdtEpqcHsLcnCZqrNXWk=; h=Date:To:cc:Subject:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=R3Ii5tsMW8YQ+H5ixbwZH9eIHoqPV9yQPQm/EKV0vZPxXWy8c2xkhb+K+HNqPuwIk Mmmj76pgTNL8h5VwEMMtza3QWTuXWlQMtrUevHEmkVylfi2ocA9I9LqSLKUJMh4B6e b1xD1ei6s2kiOdo+ofB0qcoasxWxh0t3tC0jjS+Q= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by sourceware.org (Postfix) with ESMTPS id 380103858D1E for ; Wed, 29 Mar 2023 12:11:48 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 380103858D1E Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out2.suse.de (Postfix) with ESMTP id 4A2911FDC3; Wed, 29 Mar 2023 12:11:47 +0000 (UTC) Received: from wotan.suse.de (wotan.suse.de [10.160.0.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by relay2.suse.de (Postfix) with ESMTPS id 3165D2C141; Wed, 29 Mar 2023 12:11:47 +0000 (UTC) Date: Wed, 29 Mar 2023 12:11:47 +0000 (UTC) To: gcc-patches@gcc.gnu.org cc: Jakub Jelinek Subject: [PATCH] tree-optimization/107561 - reduce -Wstringop-overflow false positives User-Agent: Alpine 2.22 (LSU 394 2020-01-19) MIME-Version: 1.0 X-Spam-Status: No, score=-10.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, MISSING_MID, SPF_HELO_NONE, SPF_PASS, TXREP 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.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Richard Biener via Gcc-patches From: Richard Biener Reply-To: Richard Biener Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" Message-Id: <20230329121233.B0266385B538@sourceware.org> X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1761704109729583511?= X-GMAIL-MSGID: =?utf-8?q?1761704109729583511?= The following tells pointer-query to prefer a zero size when we are querying for the size range for a write into an object we've determined is of zero size. That avoids diagnostics about really varying size arguments that just get a meaningful range for example because they are multiplied by an element size. I've adjusted only one call to get_size_range since that's what I have a testcase for. I think this is the most sensible "workaround" for some of the false positives we see. Bootstrapped on x86_64-unknown-linux-gnu, testing in progress. OK if it succeeds? Thanks, Richard. PR tree-optimization/107561 * gimple-ssa-warn-access.cc (get_size_range): Add flags argument and pass it on. (check_access): When querying for the size range pass SR_ALLOW_ZERO when the known destination size is zero. * g++.dg/pr71488.C: Remove XFAILed bogus diagnostic again. * g++.dg/warn/Warray-bounds-16.C: Likewise. --- gcc/gimple-ssa-warn-access.cc | 19 +++++++++++++------ gcc/testsuite/g++.dg/pr71488.C | 1 - gcc/testsuite/g++.dg/warn/Warray-bounds-16.C | 1 - 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/gcc/gimple-ssa-warn-access.cc b/gcc/gimple-ssa-warn-access.cc index 88d44690ade..b3de4b77924 100644 --- a/gcc/gimple-ssa-warn-access.cc +++ b/gcc/gimple-ssa-warn-access.cc @@ -1198,10 +1198,10 @@ warn_for_access (location_t loc, tree func, tree expr, int opt, static void get_size_range (range_query *query, tree bound, gimple *stmt, tree range[2], - const offset_int bndrng[2]) + int flags, const offset_int bndrng[2]) { if (bound) - get_size_range (query, bound, stmt, range); + get_size_range (query, bound, stmt, range, flags); if (!bndrng || (bndrng[0] == 0 && bndrng[1] == HOST_WIDE_INT_M1U)) return; @@ -1347,7 +1347,12 @@ check_access (GimpleOrTree exp, tree dstwrite, /* Set RANGE to that of DSTWRITE if non-null, bounded by PAD->DST_BNDRNG if valid. */ gimple *stmt = pad ? pad->stmt : nullptr; - get_size_range (rvals, dstwrite, stmt, range, pad ? pad->dst_bndrng : NULL); + get_size_range (rvals, dstwrite, stmt, range, + /* If the destination has known zero size prefer a zero + size range to avoid false positives if that's a + possibility. */ + integer_zerop (dstsize) ? SR_ALLOW_ZERO : 0, + pad ? pad->dst_bndrng : NULL); tree func = get_callee_fndecl (exp); /* Read vs write access by built-ins can be determined from the const @@ -1442,7 +1447,8 @@ check_access (GimpleOrTree exp, tree dstwrite, { /* Set RANGE to that of MAXREAD, bounded by PAD->SRC_BNDRNG if PAD is nonnull and BNDRNG is valid. */ - get_size_range (rvals, maxread, stmt, range, pad ? pad->src_bndrng : NULL); + get_size_range (rvals, maxread, stmt, range, 0, + pad ? pad->src_bndrng : NULL); location_t loc = get_location (exp); tree size = dstsize; @@ -1489,7 +1495,8 @@ check_access (GimpleOrTree exp, tree dstwrite, { /* Set RANGE to that of MAXREAD, bounded by PAD->SRC_BNDRNG if PAD is nonnull and BNDRNG is valid. */ - get_size_range (rvals, maxread, stmt, range, pad ? pad->src_bndrng : NULL); + get_size_range (rvals, maxread, stmt, range, 0, + pad ? pad->src_bndrng : NULL); /* Set OVERREAD for reads starting just past the end of an object. */ overread = pad->src.sizrng[1] - pad->src.offrng[0] < pad->src_bndrng[0]; range[0] = wide_int_to_tree (sizetype, pad->src_bndrng[0]); @@ -2679,7 +2686,7 @@ pass_waccess::check_strncmp (gcall *stmt) /* Determine the range of the bound first and bail if it fails; it's cheaper than computing the size of the objects. */ tree bndrng[2] = { NULL_TREE, NULL_TREE }; - get_size_range (m_ptr_qry.rvals, bound, stmt, bndrng, adata1.src_bndrng); + get_size_range (m_ptr_qry.rvals, bound, stmt, bndrng, 0, adata1.src_bndrng); if (!bndrng[0] || integer_zerop (bndrng[0])) return; diff --git a/gcc/testsuite/g++.dg/pr71488.C b/gcc/testsuite/g++.dg/pr71488.C index a89e6465a94..ffe30f1afeb 100644 --- a/gcc/testsuite/g++.dg/pr71488.C +++ b/gcc/testsuite/g++.dg/pr71488.C @@ -3,7 +3,6 @@ // { dg-options "-O3 -std=c++11" } // { dg-additional-options "-msse4" { target sse4_runtime } } // { dg-require-effective-target c++11 } -// { dg-bogus "size 0 overflows" "pr107561" { xfail *-*-* } 0 } #include diff --git a/gcc/testsuite/g++.dg/warn/Warray-bounds-16.C b/gcc/testsuite/g++.dg/warn/Warray-bounds-16.C index 49d3437fa1b..89cbadb91c7 100644 --- a/gcc/testsuite/g++.dg/warn/Warray-bounds-16.C +++ b/gcc/testsuite/g++.dg/warn/Warray-bounds-16.C @@ -20,7 +20,6 @@ struct S for (int i = 0; i < m; i++) new (p + i) int (); /* { dg-bogus "bounds" "pr102690" { xfail *-*-* } } */ - // { dg-bogus "size 0 overflows" "pr107561" { xfail ilp32 } .-1 } } };