]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Fix handling of zero capacity regions in -Wanalyzer-allocation-size [PR106394]
authorTim Lange <mail@tim-lange.me>
Fri, 22 Jul 2022 19:44:07 +0000 (21:44 +0200)
committerTim Lange <mail@tim-lange.me>
Fri, 22 Jul 2022 19:46:51 +0000 (21:46 +0200)
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.

gcc/analyzer/region-model.cc
gcc/testsuite/gcc.dg/analyzer/pr106394.c [new file with mode: 0644]

index 5bb7112a383cddd2f320101bf7f34ba2b59d1bfa..f7df2fca2452024c622b029ff2d6f0bf71cf1c81 100644 (file)
@@ -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 (file)
index 0000000..96bb175
--- /dev/null
@@ -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;
+}