Skip to content

Commit 8dbcf21

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. Signed-off-by: Pierre-Emmanuel Patry <[email protected]>
1 parent 2449174 commit 8dbcf21

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

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

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

2032+
unsigned int res;
2033+
if (__builtin_umul_overflow (TREE_INT_CST_ELT (TYPE_SIZE_UNIT (array_type),
2034+
0),
2035+
len, &res))
2036+
{
2037+
rust_error_at (expr_locus, ErrorCode::E0080,
2038+
"the type %qs is too big for the current architecture",
2039+
array_tyty.as_string ().c_str ());
2040+
return error_mark_node;
2041+
}
2042+
20322043
// In a const context we must initialize the entire array, which entails
20332044
// allocating for each element. If the user wants a huge array, we will OOM
20342045
// and die horribly.

0 commit comments

Comments
 (0)