]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
tree-optimization/106189 - avoid division by zero exception
authorRichard Biener <rguenther@suse.de>
Mon, 25 Jul 2022 15:24:57 +0000 (17:24 +0200)
committerRichard Biener <rguenther@suse.de>
Tue, 26 Jul 2022 06:36:53 +0000 (08:36 +0200)
The diagnostic code can end up with zero sized array elements
with T[][0] and the wide-int code nicely avoids exceptions when
dividing by zero in one codepath but not in another.  The following
fixes the exception by using wide-int in both paths.

PR tree-optimization/106189
* gimple-array-bounds.cc (array_bounds_checker::check_mem_ref):
Divide using offset_ints.

* gcc.dg/pr106189.c: New testcase.

gcc/gimple-array-bounds.cc
gcc/testsuite/gcc.dg/pr106189.c [new file with mode: 0644]

index 0b48bdb0ae5d450bc5c4440addc158318ce85036..e190b93aa85310b47bcf6623593c5d3dbe7abbff 100644 (file)
@@ -534,7 +534,7 @@ array_bounds_checker::check_mem_ref (location_t location, tree ref,
   int i = 0;
   if (aref.offmax[i] < -aref.sizrng[1] || aref.offmax[i = 1] > ubound)
     {
-      HOST_WIDE_INT tmpidx = aref.offmax[i].to_shwi () / eltsize.to_shwi ();
+      HOST_WIDE_INT tmpidx = (aref.offmax[i] / eltsize).to_shwi ();
 
       if (warning_at (location, OPT_Warray_bounds,
                      "intermediate array offset %wi is outside array bounds "
diff --git a/gcc/testsuite/gcc.dg/pr106189.c b/gcc/testsuite/gcc.dg/pr106189.c
new file mode 100644 (file)
index 0000000..0eca834
--- /dev/null
@@ -0,0 +1,5 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -Warray-bounds=2 -w" } */
+
+int a_n_0_0_a[][0];
+void a_n_0_0() { T(((char *)a_n_0_0_a)[1]); }