diff --git a/gcc/range-op.cc b/gcc/range-op.cc index 086c6c19735831aea513f03179d4535202b6df69..268f6b6f02508456c7eba0236cf3a4cc0c14dece 100644 --- a/gcc/range-op.cc +++ b/gcc/range-op.cc @@ -4325,7 +4325,17 @@ operator_addr_expr::op1_range (irange &r, tree type, const irange &op2, relation_trio) const { - return operator_addr_expr::fold_range (r, type, lhs, op2); + if (empty_range_varying (r, type, lhs, op2)) + return true; + + // Return a non-null pointer of the LHS type (passed in op2), but only + // if we cant overflow, eitherwise a no-zero offset could wrap to zero. + // See PR 111009. + if (!contains_zero_p (lhs) && TYPE_OVERFLOW_UNDEFINED (type)) + r = range_nonzero (type); + else + r.set_varying (type); + return true; } // Initialize any integral operators to the primary table diff --git a/gcc/testsuite/gcc.dg/pr111009.c b/gcc/testsuite/gcc.dg/pr111009.c new file mode 100644 index 0000000000000000000000000000000000000000..3accd9ac0630756385c1807a5e4bc18674c6b2ec --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr111009.c @@ -0,0 +1,38 @@ +/* PR tree-optimization/111009 */ +/* { dg-do run } */ +/* { dg-options "-O3 -fno-strict-overflow" } */ + +struct dso { + struct dso * next; + int maj; +}; + +__attribute__((noipa)) static void __dso_id__cmp_(void) {} + +__attribute__((noipa)) +static int bug(struct dso * d, struct dso *dso) +{ + struct dso **p = &d; + struct dso *curr = 0; + + while (*p) { + curr = *p; + // prevent null deref below + if (!dso) return 1; + if (dso == curr) return 1; + + int *a = &dso->maj; + // null deref + if (!(a && *a)) __dso_id__cmp_(); + + p = &curr->next; + } + return 0; +} + +__attribute__((noipa)) +int main(void) { + struct dso d = { 0, 0, }; + bug(&d, 0); +} +