aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRichard Biener <rguenther@suse.de>2022-06-15 10:54:48 +0200
committerRichard Biener <rguenther@suse.de>2022-06-15 13:14:58 +0200
commitedb9330c29fe8a0a0b76df6fafd6a223a4d0e41f (patch)
tree26277e1e4b868300d3036ae30c845da550efa9f9
parentf4c3ce32fa54c1aefac0b3a825d8a3f73de95939 (diff)
downloadgcc-edb9330c29fe8a0a0b76df6fafd6a223a4d0e41f.zip
gcc-edb9330c29fe8a0a0b76df6fafd6a223a4d0e41f.tar.gz
gcc-edb9330c29fe8a0a0b76df6fafd6a223a4d0e41f.tar.bz2
tree-optimization/105969 - FPE with array diagnostics
For a [0][0] array we have to be careful when dividing by the element size which is zero for the outermost dimension. Luckily the division is only for an overflow check which is pointless for array size zero. 2022-06-15 Richard Biener <rguenther@suse.de> PR tree-optimization/105969 * gimple-ssa-sprintf.cc (get_origin_and_offset_r): Avoid division by zero in overflow check. * gcc.dg/pr105969.c: New testcase.
-rw-r--r--gcc/gimple-ssa-sprintf.cc2
-rw-r--r--gcc/testsuite/gcc.dg/pr105969.c13
2 files changed, 14 insertions, 1 deletions
diff --git a/gcc/gimple-ssa-sprintf.cc b/gcc/gimple-ssa-sprintf.cc
index 6bd2730..a888b5a 100644
--- a/gcc/gimple-ssa-sprintf.cc
+++ b/gcc/gimple-ssa-sprintf.cc
@@ -2319,7 +2319,7 @@ get_origin_and_offset_r (tree x, HOST_WIDE_INT *fldoff, HOST_WIDE_INT *fldsize,
if (byteoff < HOST_WIDE_INT_MAX
&& elbytes < HOST_WIDE_INT_MAX
- && byteoff / elbytes == idx)
+ && (elbytes == 0 || byteoff / elbytes == idx))
{
/* For in-bounds constant offsets into constant-sized arrays
bump up *OFF, and for what's likely arrays or structs of
diff --git a/gcc/testsuite/gcc.dg/pr105969.c b/gcc/testsuite/gcc.dg/pr105969.c
new file mode 100644
index 0000000..52c63fc
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr105969.c
@@ -0,0 +1,13 @@
+/* { dg-do compile } */
+/* { dg-options "-Wall" } */
+
+struct A
+{
+ char a[0][0][0];
+};
+extern struct A b[][2];
+void f (void)
+{
+ __builtin_sprintf (b[0][0].a[1][0], "%s", b[0][0].a[1][0]); /* { dg-warning "past the end" } */
+ /* { dg-warning "overlaps destination" "" { target *-*-* } .-1 } */
+}