From 301621747e6a60cde4bb0c265d32db82dab72a2b Mon Sep 17 00:00:00 2001 From: Turner Date: Sun, 8 Oct 2023 14:34:17 -0700 Subject: [PATCH] Fix sub_bytes code --- fuel-vm/src/interpreter/blockchain.rs | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/fuel-vm/src/interpreter/blockchain.rs b/fuel-vm/src/interpreter/blockchain.rs index e283e5a7d3..32c2d6eb90 100644 --- a/fuel-vm/src/interpreter/blockchain.rs +++ b/fuel-vm/src/interpreter/blockchain.rs @@ -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; @@ -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, @@ -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 { + 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,