]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
tree-ssa-dce: Fix calloc handling [PR118224]
authorJakub Jelinek <jakub@redhat.com>
Mon, 20 Jan 2025 09:24:18 +0000 (10:24 +0100)
committerJakub Jelinek <jakub@gcc.gnu.org>
Mon, 20 Jan 2025 09:24:18 +0000 (10:24 +0100)
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.

gcc/testsuite/gcc.dg/pr118224.c
gcc/tree-ssa-dce.cc

index 683f4dc8f631e7adaf3d1a0b0081453cddc0dca2..a254e2c2d31ebbf8fe49dc8d596711e381d18e19 100644 (file)
@@ -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);
 }
index 5b3037ac625ca065624a859a7670da6d60250dc4..be21a2d0b507e4f325ecdec2100aecab74fa68f9 100644 (file)
@@ -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;