From c41979400e65514a203049b6b5ce5c866a93a403 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Thu, 12 Mar 2020 14:39:15 +0100 Subject: [PATCH] feat(interface-types) Use include ranges to read the memory. --- .../instructions/memory_to_string.rs | 28 +++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/src/interpreter/instructions/memory_to_string.rs b/src/interpreter/instructions/memory_to_string.rs index f96c47f..e30b70c 100644 --- a/src/interpreter/instructions/memory_to_string.rs +++ b/src/interpreter/instructions/memory_to_string.rs @@ -31,7 +31,13 @@ executable_instruction!( let pointer = to_native::(&inputs[1], instruction)? as usize; let memory_view = memory.view(); - if memory_view.len() < pointer + length { + if length == 0 { + runtime.stack.push(InterfaceValue::String("".into())); + + return Ok(()) + } + + if memory_view.len() <= pointer + length - 1 { return Err(InstructionError::new( instruction, InstructionErrorKind::MemoryOutOfBoundsAccess { @@ -41,7 +47,7 @@ executable_instruction!( )); } - let data: Vec = (&memory_view[pointer..pointer + length]) + let data: Vec = (&memory_view[pointer..=pointer + length - 1]) .iter() .map(Cell::get) .collect(); @@ -78,6 +84,24 @@ mod tests { stack: [InterfaceValue::String("Hello, World!".into())], ); + test_executable_instruction!( + test_memory_to_string__empty_string = + instructions: [ + Instruction::ArgumentGet { index: 1 }, + Instruction::ArgumentGet { index: 0 }, + Instruction::MemoryToString, + ], + invocation_inputs: [ + InterfaceValue::I32(0), + InterfaceValue::I32(0), + ], + instance: Instance { + memory: Memory::new(vec![]), + ..Default::default() + }, + stack: [InterfaceValue::String("".into())], + ); + test_executable_instruction!( test_memory_to_string__read_out_of_memory = instructions: [