Skip to content

Commit

Permalink
Fix sub_bytes code
Browse files Browse the repository at this point in the history
  • Loading branch information
MitchTurner committed Oct 8, 2023
1 parent 7257740 commit 3016217
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions fuel-vm/src/interpreter/blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -508,11 +508,8 @@ where
.into_owned()
.into();
let contract_len = contract_bytes.len();
let start = contract_offset;
let finish = contract_offset
.checked_add(length_unpadded)
.ok_or(PanicReason::MemoryOverflow)?;
let contract_sub_bytes = &contract_bytes[start..finish];
let contract_sub_bytes =
read_contract_bytes(&contract_bytes, contract_offset, length_unpadded);

// Mark stack space as allocated
let new_stack = dst_range.words().end;
Expand All @@ -522,7 +519,7 @@ where
// Copy the code. Ownership checks are not used as the stack is adjusted above.
copy_from_slice_zero_fill_noownerchecks(
self.memory,
contract_sub_bytes,
&contract_sub_bytes,
dst_range.start,
contract_offset,
length,
Expand Down Expand Up @@ -557,6 +554,18 @@ where
}
}

/// Copy $rC bytes of code starting at $rB for contract.
/// If $rC is greater than the code size, zero bytes are filled in.
fn read_contract_bytes(contract_bytes: &[u8], offset: usize, length: usize) -> Vec<u8> {
let mut buf = vec![0u8; length];
if contract_bytes.len() > offset {
for (i, val) in contract_bytes[offset..].iter().enumerate().take(length) {
buf[i] = *val;
}
};
buf
}

struct BurnCtx<'vm, S> {
storage: &'vm mut S,
context: &'vm Context,
Expand Down

0 comments on commit 3016217

Please sign in to comment.