Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check capacity of vector before resizing #805

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions src/vm/errors/memory_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,6 @@ pub enum MemoryError {
CantGetMutAccessedOffset,
#[error("Failed to convert String: {0} to FieldElement")]
FailedStringToFieldElementConversion(String),
#[error("Vector capacity exceeded")]
VecCapacityExceeded,
}
8 changes: 7 additions & 1 deletion src/vm/vm_memory/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use felt::Felt;
use std::{
borrow::Cow,
collections::{HashMap, HashSet},
mem::swap,
mem::{self, swap},
};

pub struct ValidationRule(
Expand Down Expand Up @@ -63,7 +63,13 @@ impl Memory {

//Check if the element is inserted next to the last one on the segment
//Forgoing this check would allow data to be inserted in a different index

if segment.len() <= value_offset {
if (segment.capacity() + value_offset + 1) * mem::size_of::<MaybeRelocatable>()
Copy link
Contributor

Choose a reason for hiding this comment

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

This will overflow if the condition is true. I'm personally not sure it's valuable to catch this case, and certainly I don't think this is the proper place to fix it. Instead, we should make sure offsets in relocatables don't exceed 47 bits, which is how big they're specified to be.

Copy link
Contributor

@fmoletta fmoletta Feb 2, 2023

Choose a reason for hiding this comment

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

+ 1

Copy link
Contributor

Choose a reason for hiding this comment

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

I'll fix this by using try_reserve in a different PR. It should be cleaner and more robust, while also avoiding other possible points of failure (it might still fail to allocate and panic as it's implemented).
I'll refer to this PR and close it when ready.

> isize::MAX as usize
{
return Err(MemoryError::VecCapacityExceeded);
}
segment.resize(value_offset + 1, None);
}
// At this point there's *something* in there
Expand Down