diff --git a/core/src/eval/misc.rs b/core/src/eval/misc.rs index f3627b722..a44adc8e7 100644 --- a/core/src/eval/misc.rs +++ b/core/src/eval/misc.rs @@ -81,7 +81,7 @@ pub fn mload(state: &mut Machine) -> Control { pop_u256!(state, index); try_or_fail!(state.memory.resize_offset(index, U256::from(32))); let index = as_usize_or_fail!(index); - let value = H256::from_slice(&state.memory.get(index, 32)[..]); + let value = state.memory.get_h256(index); push!(state, value); Control::Continue(1) } diff --git a/core/src/memory.rs b/core/src/memory.rs index 7bb09629b..b16648133 100644 --- a/core/src/memory.rs +++ b/core/src/memory.rs @@ -2,7 +2,7 @@ use crate::{ExitError, ExitFatal}; use alloc::vec::Vec; use core::cmp::min; use core::ops::{BitAnd, Not}; -use primitive_types::U256; +use primitive_types::{H256, U256}; /// A sequencial memory. It uses Rust's `Vec` for internal /// representation. @@ -82,18 +82,27 @@ impl Memory { pub fn get(&self, offset: usize, size: usize) -> Vec { let mut ret = Vec::new(); ret.resize(size, 0); + self.get_to(offset, &mut ret); + ret + } + + /// Get `H256` from a specific offset in memory. + pub fn get_h256(&self, offset: usize) -> H256 { + let mut ret = [0; 32]; + self.get_to(offset, &mut ret); + H256(ret) + } + fn get_to(&self, offset: usize, buf: &mut [u8]) { #[allow(clippy::needless_range_loop)] - for index in 0..size { + for index in 0..buf.len() { let position = offset + index; if position >= self.data.len() { break; } - ret[index] = self.data[position]; + buf[index] = self.data[position]; } - - ret } /// Set memory region at given offset. The offset and value is considered