From: Andrew MacLeod Date: Thu, 15 May 2025 15:06:05 +0000 (-0400) Subject: Check for casts becoming UNDEFINED. X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4291071a6aa3b50800ad5fc70b5fb83cb9398237;p=thirdparty%2Fgcc.git Check for casts becoming UNDEFINED. In various situations a cast that is ultimately unreahcable may produce an UNDEFINED result, and we can't check the bounds in this case. PR tree-optimization/120277 gcc/ * range-op-ptr.cc (operator_cast::fold_range): Check if the cast if UNDEFINED before setting bounds. gcc/testsuite/ * gcc.dg/pr120277.c: New. --- diff --git a/gcc/range-op-ptr.cc b/gcc/range-op-ptr.cc index 36e9dfc20ba..6aadc9cf2c9 100644 --- a/gcc/range-op-ptr.cc +++ b/gcc/range-op-ptr.cc @@ -602,8 +602,14 @@ operator_cast::fold_range (prange &r, tree type, int_range<2> tmp = inner; tree pointer_uint_type = make_unsigned_type (TYPE_PRECISION (type)); range_cast (tmp, pointer_uint_type); - r.set (type, tmp.lower_bound (), tmp.upper_bound ()); - r.update_bitmask (tmp.get_bitmask ()); + // Casts may cause ranges to become UNDEFINED based on bitmasks. + if (tmp.undefined_p ()) + r.set_varying (type); + else + { + r.set (type, tmp.lower_bound (), tmp.upper_bound ()); + r.update_bitmask (tmp.get_bitmask ()); + } return true; } diff --git a/gcc/testsuite/gcc.dg/pr120277.c b/gcc/testsuite/gcc.dg/pr120277.c new file mode 100644 index 00000000000..f291e920db1 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr120277.c @@ -0,0 +1,21 @@ +/* { dg-do compile } */ +/* { dg-options "-O2" } */ + +int a, b; +int c(int d, long e) { + switch (d) { + case 129: + a = 1; + case 128: + break; + default: + return 1; + } + *(int *)e = 0; +} +void f(int d, long e) { c(d, e); } +void g() { + int h = b * sizeof(int); + f(h + 7, h); +} +void main() {}