aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakub Jelinek <jakub@redhat.com>2025-01-20 10:24:18 +0100
committerJakub Jelinek <jakub@gcc.gnu.org>2025-01-20 10:24:18 +0100
commitd882e48d48bf300941c3610c5af157c64ccf0a84 (patch)
tree2d5df2fad6edd550a2e9acd37bd99857fe7d1cbc
parent459816efa13d9d553a5c900336f6eef22072f1a1 (diff)
downloadgcc-d882e48d48bf300941c3610c5af157c64ccf0a84.zip
gcc-d882e48d48bf300941c3610c5af157c64ccf0a84.tar.gz
gcc-d882e48d48bf300941c3610c5af157c64ccf0a84.tar.bz2
tree-ssa-dce: Fix calloc handling [PR118224]
As reported by Dimitar, this should have been a multiplication, but wasn't caught because in the test (~(__SIZE_TYPE__) 0) / 2 is the largest accepted size and so adding 3 to it also resulted in "overflow". The following patch adds one subtest to really verify it is a multiplication and fixes the operation. 2025-01-20 Jakub Jelinek <jakub@redhat.com> PR tree-optimization/118224 * tree-ssa-dce.cc (is_removable_allocation_p): Multiply a1 by a2 instead of adding it. * gcc.dg/pr118224.c: New test.
-rw-r--r--gcc/testsuite/gcc.dg/pr118224.c2
-rw-r--r--gcc/tree-ssa-dce.cc2
2 files changed, 3 insertions, 1 deletions
diff --git a/gcc/testsuite/gcc.dg/pr118224.c b/gcc/testsuite/gcc.dg/pr118224.c
index 683f4dc..a254e2c 100644
--- a/gcc/testsuite/gcc.dg/pr118224.c
+++ b/gcc/testsuite/gcc.dg/pr118224.c
@@ -27,5 +27,7 @@ main ()
#endif
if (__builtin_calloc ((~(__SIZE_TYPE__) 0) / 2, 3))
__builtin_abort ();
+ if (__builtin_calloc ((~(__SIZE_TYPE__) 0) / 16, 64))
+ __builtin_abort ();
foo (1);
}
diff --git a/gcc/tree-ssa-dce.cc b/gcc/tree-ssa-dce.cc
index 5b3037a..be21a2d 100644
--- a/gcc/tree-ssa-dce.cc
+++ b/gcc/tree-ssa-dce.cc
@@ -331,7 +331,7 @@ is_removable_allocation_p (gcall *stmt, bool non_null_check)
return false;
if (TREE_CODE (a1) == INTEGER_CST
&& TREE_CODE (a2) == INTEGER_CST
- && (wi::to_widest (a1) + wi::to_widest (a2)
+ && (wi::to_widest (a1) * wi::to_widest (a2)
> tree_to_uhwi (TYPE_MAX_VALUE (ptrdiff_type_node))))
return false;
return true;