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
+3
View File
@@ -7,6 +7,7 @@ pub enum EvalError {
InvalidBool,
PointerOutOfBounds,
InvalidPointerAccess,
ReadBytesAsPointer,
}
pub type EvalResult<T> = Result<T, EvalError>;
@@ -19,6 +20,8 @@ fn description(&self) -> &str {
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",
}
}
+4 -4
View File
@@ -149,10 +149,10 @@ pub fn read_ptr(&self, ptr: Pointer) -> EvalResult<Pointer> {
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.