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
16 changes: 8 additions & 8 deletions vm/src/memory/paged_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,18 +104,18 @@ impl PagedMemory {
}

pub fn set_words(&mut self, address: u32, values: &[u32]) -> Result<(), MemoryError> {
if address
.checked_add(values.len() as u32 * WORD_SIZE as u32)
.is_none()
{
return Err(MemoryError::AddressCalculationOverflow);
}

if values.is_empty() {
return Ok(());
}

let end_address = address + values.len() as u32 * WORD_SIZE as u32;
let words_len_u32 =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We freely swap between u32 and usize throughout the codebase, due to the fact that it is a RISC-V32 processor this is essentially always safe. So that's much cleaner and I'd recommend just doing the cast here.

u32::try_from(values.len()).map_err(|_| MemoryError::AddressCalculationOverflow)?;
let bytes_len = words_len_u32
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should be able to chain these cleanly using map_or_else.

.checked_mul(WORD_SIZE as u32)
.ok_or(MemoryError::AddressCalculationOverflow)?;
let end_address = address
.checked_add(bytes_len)
.ok_or(MemoryError::AddressCalculationOverflow)?;

let mut current_address = address;
let mut current_index = 0;
Expand Down