]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: add checks for division by zero and left shift overflow
authorPhilip Herron <herron.philip@googlemail.com>
Mon, 2 Dec 2024 13:54:29 +0000 (13:54 +0000)
committerArthur Cohen <arthur.cohen@embecosm.com>
Fri, 21 Mar 2025 11:33:08 +0000 (12:33 +0100)
These are ported from the c-family code c-warn.cc and c/c-typchk.cc

Fixes Rust-GCC#2394

gcc/rust/ChangeLog:

* backend/rust-constexpr.cc (eval_store_expression): check for null
(eval_call_expression): remove bad warning
* rust-gcc.cc (arithmetic_or_logical_expression): add warnings

gcc/testsuite/ChangeLog:

* rust/compile/issue-2394.rs: New test.

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
gcc/rust/backend/rust-constexpr.cc
gcc/rust/rust-gcc.cc
gcc/testsuite/rust/compile/issue-2394.rs [new file with mode: 0644]

index bfd7d959aa8820a2a49ef90cce9ecde54e3bdfdf..2f2bbbd921d170072b5ae1760011c729ec5301ba 100644 (file)
@@ -2929,8 +2929,13 @@ eval_store_expression (const constexpr_ctx *ctx, tree t, bool lval,
        }
     }
 
+  if (*non_constant_p)
+    return t;
+
   /* Don't share a CONSTRUCTOR that might be changed later.  */
   init = unshare_constructor (init);
+  if (init == NULL_TREE)
+    return t;
 
   if (*valp && TREE_CODE (*valp) == CONSTRUCTOR
       && TREE_CODE (init) == CONSTRUCTOR)
@@ -3585,9 +3590,6 @@ eval_call_expression (const constexpr_ctx *ctx, tree t, bool lval,
              result = *ctx->global->values.get (res);
              if (result == NULL_TREE && !*non_constant_p)
                {
-                 if (!ctx->quiet)
-                   error ("%<constexpr%> call flows off the end "
-                          "of the function");
                  *non_constant_p = true;
                }
            }
index 59983ede97d7d2efafe10fb09672ed599c16c049..7da5e2c563772a3c6d084c08c7bfc6622179f04b 100644 (file)
@@ -1106,6 +1106,17 @@ arithmetic_or_logical_expression (ArithmeticOrLogicalOperator op, tree left,
   if (floating_point && extended_type != NULL_TREE)
     ret = convert (original_type, ret);
 
+  if (op == ArithmeticOrLogicalOperator::DIVIDE
+      && (integer_zerop (right) || fixed_zerop (right)))
+    {
+      rust_error_at (location, "division by zero");
+    }
+  else if (op == ArithmeticOrLogicalOperator::LEFT_SHIFT
+          && (compare_tree_int (right, TYPE_PRECISION (TREE_TYPE (ret))) >= 0))
+    {
+      rust_error_at (location, "left shift count >= width of type");
+    }
+
   return ret;
 }
 
diff --git a/gcc/testsuite/rust/compile/issue-2394.rs b/gcc/testsuite/rust/compile/issue-2394.rs
new file mode 100644 (file)
index 0000000..92f7afc
--- /dev/null
@@ -0,0 +1,14 @@
+const A: i32 = (1 / 0);
+// { dg-error "division by zero" "" { target *-*-* } .-1 }
+
+fn main() {
+    let a = 1 / 0;
+    // { dg-error "division by zero" "" { target *-*-* } .-1 }
+
+    let b = 3;
+    let c = b / 0;
+    // { dg-error "division by zero" "" { target *-*-* } .-1 }
+
+    let a = 1 << 500;
+    // { dg-error "left shift count >= width of type" "" { target *-*-* } .-1 }
+}