]> git.lizzy.rs Git - rust.git/commitdiff
improve out of bounds error message
authorOliver Schneider <git-spam-no-reply9815368754983@oli-obk.de>
Wed, 1 Jun 2016 09:22:37 +0000 (11:22 +0200)
committerOliver Schneider <git-spam-no-reply9815368754983@oli-obk.de>
Wed, 1 Jun 2016 09:22:37 +0000 (11:22 +0200)
src/error.rs
src/memory.rs
tests/compile-fail/errors.rs

index c82b7fa1ca61a740bc57e4cd402288d0376306ac..7d252658ba2d69d655335474c811d157f543d056 100644 (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 @@ fn cause(&self) -> Option<&Error> { None }
 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()),
         }
     }
index 7cc6a9a8e9d1f0612f9c4e8d914c016327bf2077..6cae30d39434e2bc582bec4c22727bfba431c2c4 100644 (file)
@@ -181,9 +181,9 @@ fn get_bytes_unchecked(&self, ptr: Pointer, size: usize) -> EvalResult<&[u8]> {
         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 @@ fn get_bytes_unchecked_mut(&mut self, ptr: Pointer, size: usize) -> EvalResult<&
         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])
index 1f7e3ce88c6d5977e596b4ad7fcfce014dd9f688..0cadd76cccf34e864b18ffdf46273c840759d502 100644 (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]