Skip to content

Commit 7612ad7

Browse files
committed
Vec drop and truncate: drop using raw slice *mut [T]
By creating a *mut [T] directly (without going through &mut [T]), avoid questions of validity of the contents of the slice. Consider the following risky code: ```rust unsafe { let mut v = Vec::<bool>::with_capacity(16); v.set_len(16); } ``` The intention is that with this change, the above snippet will be sound because Vec::drop does no longer produces a mutable slice of the vector's contents.
1 parent 6805906 commit 7612ad7

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

src/liballoc/vec.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -741,7 +741,7 @@ impl<T> Vec<T> {
741741
return;
742742
}
743743
let remaining_len = self.len - len;
744-
let s = slice::from_raw_parts_mut(self.as_mut_ptr().add(len), remaining_len);
744+
let s = ptr::slice_from_raw_parts_mut(self.as_mut_ptr().add(len), remaining_len);
745745
self.len = len;
746746
ptr::drop_in_place(s);
747747
}
@@ -2379,7 +2379,7 @@ unsafe impl<#[may_dangle] T> Drop for Vec<T> {
23792379
fn drop(&mut self) {
23802380
unsafe {
23812381
// use drop for [T]
2382-
ptr::drop_in_place(&mut self[..]);
2382+
ptr::drop_in_place(ptr::slice_from_raw_parts_mut(self.as_mut_ptr(), self.len))
23832383
}
23842384
// RawVec handles deallocation
23852385
}

0 commit comments

Comments
 (0)