]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Check for casts becoming UNDEFINED.
authorAndrew MacLeod <amacleod@redhat.com>
Thu, 15 May 2025 15:06:05 +0000 (11:06 -0400)
committerAndrew MacLeod <amacleod@redhat.com>
Thu, 15 May 2025 16:59:35 +0000 (12:59 -0400)
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.

gcc/range-op-ptr.cc
gcc/testsuite/gcc.dg/pr120277.c [new file with mode: 0644]

index 36e9dfc20baf49f5ddcd86dc538cc814b11c5c7f..6aadc9cf2c95d4c0b4772abd0b61475fdae73caa 100644 (file)
@@ -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 (file)
index 0000000..f291e92
--- /dev/null
@@ -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() {}