From 29516c3129dbea594ea9b68f9bdf665dda909f44 Mon Sep 17 00:00:00 2001 From: Oliver Schneider Date: Wed, 1 Jun 2016 11:22:37 +0200 Subject: [PATCH] improve out of bounds error message --- src/error.rs | 10 +++++++--- src/memory.rs | 8 ++++---- tests/compile-fail/errors.rs | 2 +- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/error.rs b/src/error.rs index c82b7fa1ca61..7d252658ba2d 100644 --- a/src/error.rs +++ b/src/error.rs @@ -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()), } } diff --git a/src/memory.rs b/src/memory.rs index 7cc6a9a8e9d1..6cae30d39434 100644 --- a/src/memory.rs +++ b/src/memory.rs @@ -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]) diff --git a/tests/compile-fail/errors.rs b/tests/compile-fail/errors.rs index 1f7e3ce88c6d..0cadd76cccf3 100644 --- a/tests/compile-fail/errors.rs +++ b/tests/compile-fail/errors.rs @@ -36,7 +36,7 @@ fn undefined_byte_read() -> u8 { #[miri_run] fn out_of_bounds_read() -> u8 { let v: Vec = 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]