Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/sema/expression/subscript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,17 @@ pub(super) fn array_subscript(
index = index.cast(&index.loc(), index_ty.deref_any(), true, ns, diagnostics)?;

let deref_ty = array_ty.deref_any();

// Subscripting a zero-length bytes type is always out of bounds.
// Reject early to avoid a u8 underflow in codegen (array_length - 1).
if let Type::Bytes(0) = deref_ty {
diagnostics.push(Diagnostic::error(
*loc,
"array subscript is out of bounds".to_string(),
));
return Err(());
}

match deref_ty {
Type::Bytes(_) | Type::Array(..) | Type::DynamicBytes | Type::Slice(_) => {
if array_ty.is_contract_storage() {
Expand Down