diff --git a/src/memory.rs b/src/memory.rs index 6b181e31063a..cf7f969be8ec 100644 --- a/src/memory.rs +++ b/src/memory.rs @@ -230,7 +230,7 @@ impl<'a, 'tcx> Memory<'a, 'tcx> { pub fn reallocate(&mut self, ptr: MemoryPointer, old_size: u64, old_align: u64, new_size: u64, new_align: u64, kind: Kind) -> EvalResult<'tcx, MemoryPointer> { use std::cmp::min; - if ptr.offset != 0 || self.get(ptr.alloc_id).is_err() { + if ptr.offset != 0 { return Err(EvalError::ReallocateNonBasePtr); } if let Ok(alloc) = self.get(ptr.alloc_id) { @@ -248,7 +248,7 @@ impl<'a, 'tcx> Memory<'a, 'tcx> { } pub fn deallocate(&mut self, ptr: MemoryPointer, size_and_align: Option<(u64, u64)>, kind: Kind) -> EvalResult<'tcx> { - if ptr.offset != 0 || self.get(ptr.alloc_id).is_err() { + if ptr.offset != 0 { return Err(EvalError::DeallocateNonBasePtr); } diff --git a/tests/compile-fail/deallocate-twice.rs b/tests/compile-fail/deallocate-twice.rs index 3c4399eaa3ed..fd3cccfd53a9 100644 --- a/tests/compile-fail/deallocate-twice.rs +++ b/tests/compile-fail/deallocate-twice.rs @@ -5,7 +5,7 @@ extern crate alloc; use alloc::heap::Heap; use alloc::allocator::*; -// error-pattern: tried to deallocate with a pointer not to the beginning of an existing object +// error-pattern: tried to deallocate dangling pointer use alloc::heap::*; fn main() { diff --git a/tests/compile-fail/reallocate-dangling.rs b/tests/compile-fail/reallocate-dangling.rs new file mode 100644 index 000000000000..54636b5d2005 --- /dev/null +++ b/tests/compile-fail/reallocate-dangling.rs @@ -0,0 +1,17 @@ +#![feature(alloc, allocator_api)] + +extern crate alloc; + +use alloc::heap::Heap; +use alloc::allocator::*; + +// error-pattern: dangling pointer was dereferenced + +use alloc::heap::*; +fn main() { + unsafe { + let x = Heap.alloc(Layout::from_size_align_unchecked(1, 1)).unwrap(); + Heap.dealloc(x, Layout::from_size_align_unchecked(1, 1)); + Heap.realloc(x, Layout::from_size_align_unchecked(1, 1), Layout::from_size_align_unchecked(1, 1)); + } +}