From 5ff14a9d8088bbdd8ba3fa45025e61c3777b95f6 Mon Sep 17 00:00:00 2001 From: Pierre-Emmanuel Patry Date: Wed, 3 Sep 2025 18:59:11 +0200 Subject: [PATCH] Emit error on array size overflow When the byte size required for an array overflow we should emit an error. gcc/rust/ChangeLog: * backend/rust-compile-expr.cc (CompileExpr::array_copied_expr): Check for overflow on array memory size and emit an error. gcc/testsuite/ChangeLog: * rust/compile/issue-3962.rs: New test. Signed-off-by: Pierre-Emmanuel Patry --- gcc/rust/backend/rust-compile-expr.cc | 11 +++++++++++ gcc/testsuite/rust/compile/issue-3962.rs | 5 +++++ 2 files changed, 16 insertions(+) create mode 100644 gcc/testsuite/rust/compile/issue-3962.rs diff --git a/gcc/rust/backend/rust-compile-expr.cc b/gcc/rust/backend/rust-compile-expr.cc index 0a627f353524..72fb4a62de42 100644 --- a/gcc/rust/backend/rust-compile-expr.cc +++ b/gcc/rust/backend/rust-compile-expr.cc @@ -2045,6 +2045,17 @@ CompileExpr::array_copied_expr (location_t expr_locus, unsigned HOST_WIDE_INT len = wi::ext (max - min + 1, precision, sign).to_uhwi (); + unsigned int res; + if (__builtin_umul_overflow (TREE_INT_CST_ELT (TYPE_SIZE_UNIT (array_type), + 0), + len, &res)) + { + rust_error_at (expr_locus, ErrorCode::E0080, + "the type %qs is too big for the current architecture", + array_tyty.as_string ().c_str ()); + return error_mark_node; + } + // In a const context we must initialize the entire array, which entails // allocating for each element. If the user wants a huge array, we will OOM // and die horribly. diff --git a/gcc/testsuite/rust/compile/issue-3962.rs b/gcc/testsuite/rust/compile/issue-3962.rs new file mode 100644 index 000000000000..446af7986d9f --- /dev/null +++ b/gcc/testsuite/rust/compile/issue-3962.rs @@ -0,0 +1,5 @@ +static FOO2: () = { + let x = [47; (1 << 47) - 1]; + // { dg-error "the type ..i32; 140737488355327.. is too big for the current architecture" "" { target x86_64-*-* } .-1 } + // { dg-error "left shift count >= width of type" "" { target i?86-*-* } .-2 } +};