diff options
| author | Vadim Petrochenkov <vadim.petrochenkov@gmail.com> | 2016-10-13 14:05:59 +0300 |
|---|---|---|
| committer | Vadim Petrochenkov <vadim.petrochenkov@gmail.com> | 2016-10-13 14:05:59 +0300 |
| commit | 348c3fb0851c155f06768e54c44990d39b6eb142 (patch) | |
| tree | 890f27aea1612950824c956501fd96ac26c9e010 /src/liballoc | |
| parent | 6d062809cb2cb19df1b2cf84ec0c73c1cd8f3c71 (diff) | |
| download | rust-348c3fb0851c155f06768e54c44990d39b6eb142.tar.gz rust-348c3fb0851c155f06768e54c44990d39b6eb142.zip | |
Add assert checking that allocation and deallocation sizes are equal
Diffstat (limited to 'src/liballoc')
| -rw-r--r-- | src/liballoc/rc.rs | 7 |
1 files changed, 4 insertions, 3 deletions
diff --git a/src/liballoc/rc.rs b/src/liballoc/rc.rs index 18a345630d1..740d13c4762 100644 --- a/src/liballoc/rc.rs +++ b/src/liballoc/rc.rs @@ -375,8 +375,8 @@ impl Rc<str> { pub fn __from_str(value: &str) -> Rc<str> { unsafe { // Allocate enough space for `RcBox<str>`. - let aligned_len = (value.len() + size_of::<usize>() - 1) / size_of::<usize>(); - let vec = RawVec::<usize>::with_capacity(2 + aligned_len); + let aligned_len = 2 + (value.len() + size_of::<usize>() - 1) / size_of::<usize>(); + let vec = RawVec::<usize>::with_capacity(aligned_len); let ptr = vec.ptr(); forget(vec); // Initialize fields of `RcBox<str>`. @@ -384,7 +384,8 @@ impl Rc<str> { *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<str> = mem::transmute([ptr as usize, value.len()]); + assert!(aligned_len * size_of::<usize>() == size_of_val(&*rcbox_ptr)); Rc { ptr: Shared::new(rcbox_ptr) } } } |
