improve out of bounds error message

This commit is contained in:
Oliver Schneider 2016-06-01 11:22:37 +02:00
parent 12c2e5fab2
commit 29516c3129
No known key found for this signature in database
GPG key ID: 56D6EEA0FC67AC46
3 changed files with 12 additions and 8 deletions

View file

@ -1,6 +1,7 @@
use std::error::Error;
use std::fmt;
use rustc::mir::repr as mir;
use memory::Pointer;
#[derive(Clone, Debug)]
pub enum EvalError {
@ -8,9 +9,9 @@ pub enum EvalError {
InvalidBool,
InvalidDiscriminant,
PointerOutOfBounds {
offset: usize,
ptr: Pointer,
size: usize,
len: usize,
allocation_size: usize,
},
ReadPointerAsBytes,
ReadBytesAsPointer,
@ -53,7 +54,10 @@ impl Error for EvalError {
impl fmt::Display for EvalError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
EvalError::PointerOutOfBounds { offset, size, len } => write!(f, "pointer offset ({} + {}) outside bounds ({}) of allocation", offset, size, len),
EvalError::PointerOutOfBounds { ptr, size, allocation_size } => {
write!(f, "memory access of {}..{} outside bounds of allocation {} which has size {}",
ptr.offset, ptr.offset + size, ptr.alloc_id, allocation_size)
},
_ => write!(f, "{}", self.description()),
}
}

View file

@ -181,9 +181,9 @@ impl Memory {
let alloc = self.get(ptr.alloc_id)?;
if ptr.offset + size > alloc.bytes.len() {
return Err(EvalError::PointerOutOfBounds {
offset: ptr.offset,
ptr: ptr,
size: size,
len: alloc.bytes.len(),
allocation_size: alloc.bytes.len(),
});
}
Ok(&alloc.bytes[ptr.offset..ptr.offset + size])
@ -193,9 +193,9 @@ impl Memory {
let alloc = self.get_mut(ptr.alloc_id)?;
if ptr.offset + size > alloc.bytes.len() {
return Err(EvalError::PointerOutOfBounds {
offset: ptr.offset,
ptr: ptr,
size: size,
len: alloc.bytes.len(),
allocation_size: alloc.bytes.len(),
});
}
Ok(&mut alloc.bytes[ptr.offset..ptr.offset + size])

View file

@ -36,7 +36,7 @@ fn undefined_byte_read() -> u8 {
#[miri_run]
fn out_of_bounds_read() -> u8 {
let v: Vec<u8> = vec![1, 2];
unsafe { *v.get_unchecked(5) } //~ ERROR: pointer offset (5 + 1) outside bounds (2) of allocation
unsafe { *v.get_unchecked(5) } //~ ERROR: memory access of 5..6 outside bounds of allocation 11 which has size 2
}
#[miri_run]