Skip to content

Commit

Permalink
fix(levm): don't update memory in extcodecopy if offset is larger tha… (
Browse files Browse the repository at this point in the history
#1471)

**Motivation**
Postpone `extcodcopy`'s cast to usize until it's checked that it can
fit.
<!-- Why does this pull request exist? What are its goals? -->

**Description**
extcodecopy receives as input an offset. That offset can be of size
u256. This need to be casted to usize to index the code it want to
access. However, it is done too early; which causes a problem when the
offset is larger than the code itself.
<!-- A clear and concise general description of the changes this PR
introduces -->

<!-- Link to issues: Resolves #111, Resolves #222 -->

Closes: #1469
  • Loading branch information
lima-limon-inc authored Dec 12, 2024
1 parent 3611c41 commit 5e6f09e
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions crates/vm/levm/src/opcode_handlers/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,11 +293,7 @@ impl VM {
) -> Result<OpcodeSuccess, VMError> {
let address = word_to_address(current_call_frame.stack.pop()?);
let dest_offset = current_call_frame.stack.pop()?;
let offset: usize = current_call_frame
.stack
.pop()?
.try_into()
.map_err(|_| VMError::VeryLargeNumber)?;
let offset = current_call_frame.stack.pop()?;
let size: usize = current_call_frame
.stack
.pop()?
Expand All @@ -323,15 +319,20 @@ impl VM {
}

let mut data = vec![0u8; size];
for (i, byte) in account_info
.bytecode
.iter()
.skip(offset)
.take(size)
.enumerate()
{
if let Some(data_byte) = data.get_mut(i) {
*data_byte = *byte;
if offset < account_info.bytecode.len().into() {
let offset: usize = offset
.try_into()
.map_err(|_| VMError::Internal(InternalError::ConversionError))?;
for (i, byte) in account_info
.bytecode
.iter()
.skip(offset)
.take(size)
.enumerate()
{
if let Some(data_byte) = data.get_mut(i) {
*data_byte = *byte;
}
}
}

Expand Down

0 comments on commit 5e6f09e

Please sign in to comment.