public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Tim Lange <tlange@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r13-1802] Fix handling of zero capacity regions in -Wanalyzer-allocation-size [PR106394]
Date: Fri, 22 Jul 2022 19:50:04 +0000 (GMT)	[thread overview]
Message-ID: <20220722195004.46641385828D@sourceware.org> (raw)

https://gcc.gnu.org/g:b4cc945c045db74f719ab030969806c14e2d5fc3

commit r13-1802-gb4cc945c045db74f719ab030969806c14e2d5fc3
Author: Tim Lange <mail@tim-lange.me>
Date:   Fri Jul 22 21:44:07 2022 +0200

    Fix handling of zero capacity regions in -Wanalyzer-allocation-size [PR106394]
    
    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  <mail@tim-lange.me>
    
    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.

Diff:
---
 gcc/analyzer/region-model.cc             |  2 +-
 gcc/testsuite/gcc.dg/analyzer/pr106394.c | 19 +++++++++++++++++++
 2 files changed, 20 insertions(+), 1 deletion(-)

diff --git a/gcc/analyzer/region-model.cc b/gcc/analyzer/region-model.cc
index 5bb7112a383..f7df2fca245 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;
+}


                 reply	other threads:[~2022-07-22 19:50 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220722195004.46641385828D@sourceware.org \
    --to=tlange@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).