Change invalid pointer read panic into Err.

This commit is contained in:
Scott Olson 2016-03-17 07:24:10 -06:00
parent 6477a5c694
commit 0b37be71c2
2 changed files with 7 additions and 4 deletions

View file

@ -7,6 +7,7 @@ pub enum EvalError {
InvalidBool,
PointerOutOfBounds,
InvalidPointerAccess,
ReadBytesAsPointer,
}
pub type EvalResult<T> = Result<T, EvalError>;
@ -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",
}
}

View file

@ -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.