Merge pull request #254 from RalfJung/dangling

Remove reundant dangling checks in {r,d}eallocate
This commit is contained in:
Oliver Schneider 2017-07-18 23:34:09 +02:00 committed by GitHub
commit 2d5c4196f1
3 changed files with 20 additions and 3 deletions

View file

@ -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);
}

View file

@ -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() {

View file

@ -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));
}
}