From 0b37be71c29cc35173c60a643cf44af11ef2f769 Mon Sep 17 00:00:00 2001 From: Scott Olson Date: Thu, 17 Mar 2016 07:24:10 -0600 Subject: [PATCH] Change invalid pointer read panic into Err. --- src/error.rs | 3 +++ src/memory.rs | 8 ++++---- 2 files changed, 7 insertions(+), 4 deletions(-) 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.