diff --git a/src/error.rs b/src/error.rs index a05250e3b548..9d8830f0d534 100644 --- a/src/error.rs +++ b/src/error.rs @@ -7,6 +7,7 @@ pub enum EvalError { InvalidBool, PointerOutOfBounds, InvalidPointerAccess, + ReadBytesAsPointer, } pub type EvalResult = Result; @@ -19,6 +20,8 @@ impl Error for EvalError { EvalError::PointerOutOfBounds => "pointer offset outside bounds of allocation", EvalError::InvalidPointerAccess => "a raw memory access tried to access part of a pointer value as bytes", + EvalError::ReadBytesAsPointer => + "attempted to read some raw bytes as a pointer address", } } diff --git a/src/memory.rs b/src/memory.rs index ae927865b4dd..d9d1a60111f4 100644 --- a/src/memory.rs +++ b/src/memory.rs @@ -149,10 +149,10 @@ impl Memory { let bytes = &alloc.bytes[ptr.offset..ptr.offset + POINTER_SIZE]; let offset = byteorder::NativeEndian::read_u64(bytes) as usize; - // TODO(tsion): Return an EvalError here instead of panicking. - let alloc_id = *alloc.relocations.get(&ptr.offset).unwrap(); - - Ok(Pointer { alloc_id: alloc_id, offset: offset }) + match alloc.relocations.get(&ptr.offset) { + Some(&alloc_id) => Ok(Pointer { alloc_id: alloc_id, offset: offset }), + None => Err(EvalError::ReadBytesAsPointer), + } } // TODO(tsion): Detect invalid writes here and elsewhere.