]> git.lizzy.rs Git - rust.git/commitdiff
remove reundant dangling checks in {r,d}eallocate
authorRalf Jung <post@ralfj.de>
Tue, 18 Jul 2017 20:50:54 +0000 (13:50 -0700)
committerRalf Jung <post@ralfj.de>
Tue, 18 Jul 2017 20:50:54 +0000 (13:50 -0700)
src/memory.rs
tests/compile-fail/deallocate-twice.rs
tests/compile-fail/reallocate-dangling.rs [new file with mode: 0644]

index 6b181e31063ab5e9f833cee370ca2bbd35936d53..cf7f969be8ecda3683a66fd4adc3d473c09d1173 100644 (file)
@@ -230,7 +230,7 @@ pub fn allocate(&mut self, size: u64, align: u64, kind: Kind) -> EvalResult<'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 @@ pub fn reallocate(&mut self, ptr: MemoryPointer, old_size: u64, old_align: u64,
     }
 
     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);
         }
 
index 3c4399eaa3ed63e63d15ff3443bbd958fa77a0cd..fd3cccfd53a916a949c54f730de726717291da55 100644 (file)
@@ -5,7 +5,7 @@
 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 (file)
index 0000000..54636b5
--- /dev/null
@@ -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));
+    }
+}