diff options
| author | Oliver Schneider <oli-obk@users.noreply.github.com> | 2017-07-18 23:34:09 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2017-07-18 23:34:09 +0200 |
| commit | 2d5c4196f17fc8d09ac2c434f2a49cdb07e7f4bc (patch) | |
| tree | bcb6fc3810fcff18e688ed83020e54b654591a5c | |
| parent | 824438bda99462871f81ae8723dd53f6c2b9f6ca (diff) | |
| parent | ff9192e3469bd0eca4207a6c55b372384d5d7f5a (diff) | |
| download | rust-2d5c4196f17fc8d09ac2c434f2a49cdb07e7f4bc.tar.gz rust-2d5c4196f17fc8d09ac2c434f2a49cdb07e7f4bc.zip | |
Merge pull request #254 from RalfJung/dangling
Remove reundant dangling checks in {r,d}eallocate| -rw-r--r-- | src/memory.rs | 4 | ||||
| -rw-r--r-- | tests/compile-fail/deallocate-twice.rs | 2 | ||||
| -rw-r--r-- | tests/compile-fail/reallocate-dangling.rs | 17 |
3 files changed, 20 insertions, 3 deletions
diff --git a/src/memory.rs b/src/memory.rs index 6b181e31063..cf7f969be8e 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 3c4399eaa3e..fd3cccfd53a 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 00000000000..54636b5d200 --- /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)); + } +} |
