fix(interpreter): fix safeguard for memory write_raw()

This commit is contained in:
2025-07-02 17:16:01 +09:00
parent cf3383c23a
commit 709d2a2639

View File

@@ -20,14 +20,18 @@ impl Memory {
/// Safely writes a [`ImmediateOperand`] into an index of memory. /// Safely writes a [`ImmediateOperand`] into an index of memory.
/// Warning: Does access at `addr`, not `DS:addr`! /// Warning: Does access at `addr`, not `DS:addr`!
pub fn write_raw(&mut self, addr: Word, val: ImmediateOperand) -> Result<(), InterpreterError> { pub fn write_raw(&mut self, addr: Word, val: ImmediateOperand) -> Result<(), InterpreterError> {
if (addr + 1) as usize > MEMORY_SIZE { match val {
return Err(InterpreterError::MemoryOutOfBound(addr)); ImmediateOperand::Byte(b) => {
} else { if addr as usize > MEMORY_SIZE {
match val { return Err(InterpreterError::MemoryOutOfBound(addr));
ImmediateOperand::Byte(b) => { } else {
self.raw[addr as usize] = b; 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(); let [low, high] = w.to_le_bytes();
self.raw[addr as usize] = low; self.raw[addr as usize] = low;
self.raw[(addr + 1) as usize] = high; self.raw[(addr + 1) as usize] = high;