From patchwork Fri Jul 22 14:27:07 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tim Lange X-Patchwork-Id: 131 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f503:0:0:0:0:0 with SMTP id q3csp271817wro; Fri, 22 Jul 2022 07:28:26 -0700 (PDT) X-Google-Smtp-Source: AGRyM1vdhH0ZgeP3RV1PuiJJIorZQwjA0jvLoPA7aZ9FEyz4iUYOaH2Bs72JyPFQMx6Vv0b9DhhB X-Received: by 2002:a05:6402:291c:b0:43b:d177:c59 with SMTP id ee28-20020a056402291c00b0043bd1770c59mr130389edb.370.1658500106214; Fri, 22 Jul 2022 07:28:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1658500106; cv=none; d=google.com; s=arc-20160816; b=PCaceoKcw8BF+kHVi/q5q9witXbz6bStbc1Au8yrar8Y32t0m3Oy6tNxm+098Ua3+p YW7Rw2FCkpAdBXOWI1lG6FKvwgkhy9N9gfFOPQ4CQPsYa6t+arP0icfbgn7+/KxNNd2i g4ts+Iv4NQbNBdF9+E5xEl1OE4cm9WBJQHGjX2fqfshlobLBlx55lHzPuI98E4iEsT4x 8cjUZr2pIcigUFIzKSK6L3qhw2pdXERE8B/tIfyBydQfAUwmmJISOtO3u2cKkY/1QrLf cmHA6L5I9YPXi0zdIaKf9TnZeDIh2D9+FBdUmtiZXcsHNLH7i94Xl0PfN7Fif9n9rdi2 Ki1Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:to:from:dkim-signature :dmarc-filter:delivered-to; bh=gSRq7gtXQLIl59CvCtYMjhzd/HivQC/dhpHy/Y/6M7c=; b=ZfLYoj75FrUpzZ2UputcdRUEqV6nsh7V5/oe+m18PGkJgXVWUja6rsxC1rBIWIWp/f vx24JBCq0pAdBvhu/eOuA25lIwTmzsANJgqzlC+X/aNAt+VI/9IJqr4OJ7epXkCBmjqV zbh86UWXgh5bSkXy/Z4Q4hWRNDTEjZr/LGaOAm0HvVI+icTpvdnY3QQFN71V1FyaZZ/6 PVyULTvx9ImI719z8HzworRI4SakupaNuchLCUlEmmg8vQz9JU54EqoKrn1LYFQFpOS7 ZFOXlBa6bJrMwcvVAFVDvJv0zRKgrEzij+RHXr3VNhYqCXth7cwbD2N6ZHPq/1PF45y0 PVbQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@tim-lange.me header.s=default2108 header.b=k6CAZ+pB; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id n10-20020a50934a000000b0043aa8412576si5939352eda.227.2022.07.22.07.28.25 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Jul 2022 07:28:26 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=fail header.i=@tim-lange.me header.s=default2108 header.b=k6CAZ+pB; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id F226A3834E5E for ; Fri, 22 Jul 2022 14:28:21 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from www523.your-server.de (www523.your-server.de [159.69.224.22]) by sourceware.org (Postfix) with ESMTPS id B898A3857C5D for ; Fri, 22 Jul 2022 14:27:57 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org B898A3857C5D Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=tim-lange.me Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=tim-lange.me DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=tim-lange.me; s=default2108; h=Content-Transfer-Encoding:MIME-Version: Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:In-Reply-To:References; bh=gSRq7gtXQLIl59CvCtYMjhzd/HivQC/dhpHy/Y/6M7c=; b=k6CAZ+pBavZ22+a4MoB8yH+iFa 6h4m3xJAsECsv48IkqGWQc5V2Li1w7lLOGncmm9/3WV6lnyfg3C420i4ukHqYupbeS7zfFGAk2k89 4/+SIpDknnZCCk/RWFd23URKIGLgAMg4qzUdAQW1Wd5PHFxchwSVseGEzPi0YbEEifNVX1euG9/Vp LotAC2L2rBDbajiZTjHjw3aRUxFt8LJ8qvugCbaPz2811DYfaR9Ibn6TVM7W/IIPs6dbw7qMZOjRm d7pS4uSE/a71j1dFNvb7S7j71o3911X5H66Ypt7AGCs97+f1NrI8zK77IFhvPVb2OfviOkeWsYyd8 84lPR2xg==; Received: from sslproxy03.your-server.de ([88.198.220.132]) by www523.your-server.de with esmtpsa (TLSv1.3:TLS_AES_256_GCM_SHA384:256) (Exim 4.92.3) (envelope-from ) id 1oEtdD-000G0A-Ht; Fri, 22 Jul 2022 16:27:55 +0200 Received: from [2a02:908:1861:d6a0::f4e2] (helo=fedora..) by sslproxy03.your-server.de with esmtpsa (TLSv1.3:TLS_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oEtdD-000INz-Bg; Fri, 22 Jul 2022 16:27:55 +0200 From: Tim Lange To: gcc-patches@gcc.gnu.org, dmalcolm@redhat.com Subject: [PATCH] Fix handling of zero capacity regions in -Wanalyzer-allocation-size [PR106394] Date: Fri, 22 Jul 2022 16:27:07 +0200 Message-Id: <20220722142707.178390-1-mail@tim-lange.me> X-Mailer: git-send-email 2.36.1 MIME-Version: 1.0 X-Authenticated-Sender: mail@tim-lange.me X-Virus-Scanned: Clear (ClamAV 0.103.6/26609/Fri Jul 22 09:56:47 2022) X-Spam-Status: No, score=-12.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_INFOUSMEBIZ, 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: , Cc: Tim Lange Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1739063407374611390?= X-GMAIL-MSGID: =?utf-8?q?1739063407374611390?= This patch unifies the handling of zero capacity regions for structs and other types in the allocation size checker. Regression-tested on x86_64 Linux. 2022-07-22 Tim Lange gcc/analyzer/ChangeLog: PR analyzer/106394 * region-model.cc (capacity_compatible_with_type): Always return true if alloc_size is zero. gcc/testsuite/ChangeLog: PR analyzer/106394 * gcc.dg/analyzer/pr106394.c: New test. --- gcc/analyzer/region-model.cc | 2 +- gcc/testsuite/gcc.dg/analyzer/pr106394.c | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/gcc.dg/analyzer/pr106394.c -- 2.36.1 diff --git a/gcc/analyzer/region-model.cc b/gcc/analyzer/region-model.cc index 8b7b4e1f697..e01c30407c4 100644 --- a/gcc/analyzer/region-model.cc +++ b/gcc/analyzer/region-model.cc @@ -2956,7 +2956,7 @@ capacity_compatible_with_type (tree cst, tree pointee_size_tree, unsigned HOST_WIDE_INT alloc_size = TREE_INT_CST_LOW (cst); if (is_struct) - return alloc_size >= pointee_size; + return alloc_size == 0 || alloc_size >= pointee_size; return alloc_size % pointee_size == 0; } diff --git a/gcc/testsuite/gcc.dg/analyzer/pr106394.c b/gcc/testsuite/gcc.dg/analyzer/pr106394.c new file mode 100644 index 00000000000..96bb175fc14 --- /dev/null +++ b/gcc/testsuite/gcc.dg/analyzer/pr106394.c @@ -0,0 +1,19 @@ +struct msm_gpu { + // [...snip...] + const struct msm_gpu_perfcntr *perfcntrs; + // [...snip...] +}; + +struct msm_gpu_perfcntr { + // [...snip...] + const char *name; +}; + +static const struct msm_gpu_perfcntr perfcntrs[] = {}; + +struct msm_gpu *test(struct msm_gpu *gpu) { + // [...snip...] + gpu->perfcntrs = perfcntrs; + // [...snip...] + return gpu; +}