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.
This commit is contained in:
Tim Lange 2022-07-22 21:44:07 +02:00
parent 64cb87b238
commit b4cc945c04
2 changed files with 20 additions and 1 deletions

View 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;
}

View file

@ -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;
}