Skip to content

Commit 6be34d4

Browse files
committed
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 <pierre-emmanuel.patry@embecosm.com>
1 parent c4d2abe commit 6be34d4

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

gcc/rust/backend/rust-compile-expr.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2056,6 +2056,17 @@ CompileExpr::array_copied_expr (location_t expr_locus,
20562056
unsigned HOST_WIDE_INT len
20572057
= wi::ext (max - min + 1, precision, sign).to_uhwi ();
20582058

2059+
unsigned int res;
2060+
if (__builtin_umul_overflow (TREE_INT_CST_ELT (TYPE_SIZE_UNIT (array_type),
2061+
0),
2062+
len, &res))
2063+
{
2064+
rust_error_at (expr_locus, ErrorCode::E0080,
2065+
"the type %qs is too big for the current architecture",
2066+
array_tyty.as_string ().c_str ());
2067+
return error_mark_node;
2068+
}
2069+
20592070
// In a const context we must initialize the entire array, which entails
20602071
// allocating for each element. If the user wants a huge array, we will OOM
20612072
// and die horribly.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
static FOO2: () = {
2+
let x = [47; (1 << 47) - 1];
3+
// { dg-error "the type ..i32; 140737488355327.. is too big for the current architecture" "" { target x86_64-*-* } .-1 }
4+
// { dg-error "left shift count >= width of type" "" { target x86_64-*-* } .-2 }
5+
};

0 commit comments

Comments
 (0)