From 559461094426e0cd1a57495f573b4cfeb2136eb5 Mon Sep 17 00:00:00 2001 From: maximopalopoli Date: Thu, 14 Nov 2024 12:06:48 -0300 Subject: [PATCH] Remove implicit conversions in callframe.rs --- crates/vm/levm/src/call_frame.rs | 18 +++++++++++------- .../stack_memory_storage_flow.rs | 4 ++-- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/crates/vm/levm/src/call_frame.rs b/crates/vm/levm/src/call_frame.rs index 47c3b32b9..c399ce9ed 100644 --- a/crates/vm/levm/src/call_frame.rs +++ b/crates/vm/levm/src/call_frame.rs @@ -133,16 +133,20 @@ impl CallFrame { } /// Jump to the given address, returns false if the jump position wasn't a JUMPDEST - pub fn jump(&mut self, jump_address: U256) -> bool { - if !self.valid_jump(jump_address) { - return false; + pub fn jump(&mut self, jump_address: U256) -> Result { + let jump_address_usize = jump_address + .try_into() + .map_err(|_err| VMError::VeryLargeNumber)?; + + if !self.valid_jump(jump_address_usize) { + return Ok(false); } - self.pc = jump_address.as_usize(); - true + self.pc = jump_address_usize; + Ok(true) } - fn valid_jump(&self, jump_address: U256) -> bool { - self.opcode_at(jump_address.as_usize()) + fn valid_jump(&self, jump_address: usize) -> bool { + self.opcode_at(jump_address) .map(|opcode| opcode.eq(&Opcode::JUMPDEST)) .is_some_and(|is_jumpdest| is_jumpdest) } diff --git a/crates/vm/levm/src/opcode_handlers/stack_memory_storage_flow.rs b/crates/vm/levm/src/opcode_handlers/stack_memory_storage_flow.rs index 6d71ea29b..bf946ea9f 100644 --- a/crates/vm/levm/src/opcode_handlers/stack_memory_storage_flow.rs +++ b/crates/vm/levm/src/opcode_handlers/stack_memory_storage_flow.rs @@ -344,7 +344,7 @@ impl VM { self.increase_consumed_gas(current_call_frame, gas_cost::JUMP)?; let jump_address = current_call_frame.stack.pop()?; - if !current_call_frame.jump(jump_address) { + if !current_call_frame.jump(jump_address)? { return Err(VMError::InvalidJump); } @@ -359,7 +359,7 @@ impl VM { let jump_address = current_call_frame.stack.pop()?; let condition = current_call_frame.stack.pop()?; - if condition != U256::zero() && !current_call_frame.jump(jump_address) { + if condition != U256::zero() && !current_call_frame.jump(jump_address)? { return Err(VMError::InvalidJump); }