From 709d2a2639ba172fb6408a7470f8248085000d8e Mon Sep 17 00:00:00 2001 From: Marco Thomas Date: Wed, 2 Jul 2025 17:16:01 +0900 Subject: [PATCH] fix(interpreter): fix safeguard for memory `write_raw()` --- src/interpreter/memory.rs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/interpreter/memory.rs b/src/interpreter/memory.rs index e8e8eea..ac7e90b 100644 --- a/src/interpreter/memory.rs +++ b/src/interpreter/memory.rs @@ -20,14 +20,18 @@ impl Memory { /// Safely writes a [`ImmediateOperand`] into an index of memory. /// Warning: Does access at `addr`, not `DS:addr`! pub fn write_raw(&mut self, addr: Word, val: ImmediateOperand) -> Result<(), InterpreterError> { - if (addr + 1) as usize > MEMORY_SIZE { - return Err(InterpreterError::MemoryOutOfBound(addr)); - } else { - match val { - ImmediateOperand::Byte(b) => { + match val { + ImmediateOperand::Byte(b) => { + if addr as usize > MEMORY_SIZE { + return Err(InterpreterError::MemoryOutOfBound(addr)); + } else { self.raw[addr as usize] = b; } - ImmediateOperand::Word(w) => { + } + ImmediateOperand::Word(w) => { + if (addr + 1) as usize > MEMORY_SIZE { + return Err(InterpreterError::MemoryOutOfBound(addr)); + } else { let [low, high] = w.to_le_bytes(); self.raw[addr as usize] = low; self.raw[(addr + 1) as usize] = high;