Skip to content

Commit

Permalink
Rollup merge of rust-lang#25254 - cgaebel:check-sizes-on-allocate, r=…
Browse files Browse the repository at this point in the history
…Gankro

They're only enabled in debug builds, but a panic is usually more
welcome than UB in debug builds.

Previous review at rust-lang#22069

r? @gankro
cc @huon
  • Loading branch information
steveklabnik committed May 12, 2015
2 parents 4b88e8f + 101b25c commit 30b5271
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/liballoc/heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use core::{isize, usize};

#[inline(always)]
fn check_size_and_alignment(size: usize, align: usize) {
debug_assert!(size != 0);
debug_assert!(size <= isize::MAX as usize, "Tried to allocate too much: {} bytes", size);
debug_assert!(usize::is_power_of_two(align), "Invalid alignment of allocation: {}", align);
}

// FIXME: #13996: mark the `allocate` and `reallocate` return value as `noalias`

/// Return a pointer to `size` bytes of memory aligned to `align`.
Expand All @@ -19,6 +28,7 @@
/// size on the platform.
#[inline]
pub unsafe fn allocate(size: usize, align: usize) -> *mut u8 {
check_size_and_alignment(size, align);
imp::allocate(size, align)
}

Expand All @@ -38,6 +48,7 @@ pub unsafe fn allocate(size: usize, align: usize) -> *mut u8 {
/// any value in range_inclusive(requested_size, usable_size).
#[inline]
pub unsafe fn reallocate(ptr: *mut u8, old_size: usize, size: usize, align: usize) -> *mut u8 {
check_size_and_alignment(size, align);
imp::reallocate(ptr, old_size, size, align)
}

Expand All @@ -56,6 +67,7 @@ pub unsafe fn reallocate(ptr: *mut u8, old_size: usize, size: usize, align: usiz
#[inline]
pub unsafe fn reallocate_inplace(ptr: *mut u8, old_size: usize, size: usize,
align: usize) -> usize {
check_size_and_alignment(size, align);
imp::reallocate_inplace(ptr, old_size, size, align)
}

Expand Down

0 comments on commit 30b5271

Please sign in to comment.