Skip to content

Commit 348c3fb

Browse files
committed
Add assert checking that allocation and deallocation sizes are equal
1 parent 6d06280 commit 348c3fb

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

src/liballoc/rc.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -375,16 +375,17 @@ impl Rc<str> {
375375
pub fn __from_str(value: &str) -> Rc<str> {
376376
unsafe {
377377
// Allocate enough space for `RcBox<str>`.
378-
let aligned_len = (value.len() + size_of::<usize>() - 1) / size_of::<usize>();
379-
let vec = RawVec::<usize>::with_capacity(2 + aligned_len);
378+
let aligned_len = 2 + (value.len() + size_of::<usize>() - 1) / size_of::<usize>();
379+
let vec = RawVec::<usize>::with_capacity(aligned_len);
380380
let ptr = vec.ptr();
381381
forget(vec);
382382
// Initialize fields of `RcBox<str>`.
383383
*ptr.offset(0) = 1; // strong: Cell::new(1)
384384
*ptr.offset(1) = 1; // weak: Cell::new(1)
385385
ptr::copy_nonoverlapping(value.as_ptr(), ptr.offset(2) as *mut u8, value.len());
386386
// Combine the allocation address and the string length into a fat pointer to `RcBox`.
387-
let rcbox_ptr = mem::transmute([ptr as usize, value.len()]);
387+
let rcbox_ptr: *mut RcBox<str> = mem::transmute([ptr as usize, value.len()]);
388+
assert!(aligned_len * size_of::<usize>() == size_of_val(&*rcbox_ptr));
388389
Rc { ptr: Shared::new(rcbox_ptr) }
389390
}
390391
}

0 commit comments

Comments
 (0)