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