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.
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
--- /dev/null
+/* { 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 } */
+}