From 348c3fb0851c155f06768e54c44990d39b6eb142 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Thu, 13 Oct 2016 14:05:59 +0300 Subject: [PATCH] Add assert checking that allocation and deallocation sizes are equal --- src/liballoc/rc.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/liballoc/rc.rs b/src/liballoc/rc.rs index 18a345630d13..740d13c47622 100644 --- a/src/liballoc/rc.rs +++ b/src/liballoc/rc.rs @@ -375,8 +375,8 @@ impl Rc { pub fn __from_str(value: &str) -> Rc { unsafe { // Allocate enough space for `RcBox`. - let aligned_len = (value.len() + size_of::() - 1) / size_of::(); - let vec = RawVec::::with_capacity(2 + aligned_len); + let aligned_len = 2 + (value.len() + size_of::() - 1) / size_of::(); + let vec = RawVec::::with_capacity(aligned_len); let ptr = vec.ptr(); forget(vec); // Initialize fields of `RcBox`. @@ -384,7 +384,8 @@ impl Rc { *ptr.offset(1) = 1; // weak: Cell::new(1) ptr::copy_nonoverlapping(value.as_ptr(), ptr.offset(2) as *mut u8, value.len()); // Combine the allocation address and the string length into a fat pointer to `RcBox`. - let rcbox_ptr = mem::transmute([ptr as usize, value.len()]); + let rcbox_ptr: *mut RcBox = mem::transmute([ptr as usize, value.len()]); + assert!(aligned_len * size_of::() == size_of_val(&*rcbox_ptr)); Rc { ptr: Shared::new(rcbox_ptr) } } }