Skip to content

Commit

Permalink
fix: clamp options
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon-Becker committed Jan 30, 2025
1 parent 5d87f63 commit 05653c7
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions crates/vm/src/core/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -804,7 +804,7 @@ impl VM {
// Safely convert U256 to usize
let i: usize = i.try_into().unwrap_or(usize::MAX);

let result = if i + 32 > self.calldata.len() {
let result = if i.saturating_add(32) > self.calldata.len() {
let mut value = [0u8; 32];

if i <= self.calldata.len() {
Expand Down Expand Up @@ -881,6 +881,7 @@ impl VM {
let offset: usize = offset.try_into().unwrap_or(usize::MAX);
let size: usize = size.try_into().unwrap_or(usize::MAX);

// clamp values to bytecode length
let value_offset_safe = offset.saturating_add(size).min(self.bytecode.len());
let mut value =
self.bytecode.get(offset..value_offset_safe).unwrap_or(&[]).to_owned();
Expand Down Expand Up @@ -932,8 +933,8 @@ impl VM {
let size = self.stack.pop()?.value;

// Safely convert U256 to usize
let dest_offset: usize = dest_offset.try_into().unwrap_or(usize::MAX);
let size: usize = size.try_into().unwrap_or(usize::MAX);
let dest_offset: usize = dest_offset.try_into().unwrap_or(0);
let size: usize = size.try_into().unwrap_or(256);

let mut value = Vec::with_capacity(size);
value.fill(0xff);
Expand Down Expand Up @@ -971,8 +972,8 @@ impl VM {
let size = self.stack.pop()?.value;

// Safely convert U256 to usize
let dest_offset: usize = dest_offset.try_into().unwrap_or(usize::MAX);
let size: usize = size.try_into().unwrap_or(usize::MAX);
let dest_offset: usize = dest_offset.try_into().unwrap_or(0);
let size: usize = size.try_into().unwrap_or(256);

let mut value = Vec::with_capacity(size);
value.fill(0xff);
Expand Down

0 comments on commit 05653c7

Please sign in to comment.