Skip to content

Commit 4f80816

Browse files
committed
Implement split_at_spare_mut directly
The previous implementation used slice::as_mut_ptr_range to derive the pointer for the spare capacity slice. This is invalid, because that pointer is derived from the initialized region, so it does not have provenance over the uninitialized region.
1 parent daf2204 commit 4f80816

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

library/alloc/src/vec/mod.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -2141,12 +2141,15 @@ impl<T, A: Allocator> Vec<T, A> {
21412141
unsafe fn split_at_spare_mut_with_len(
21422142
&mut self,
21432143
) -> (&mut [T], &mut [MaybeUninit<T>], &mut usize) {
2144-
let Range { start: ptr, end: spare_ptr } = self.as_mut_ptr_range();
2144+
let ptr = self.as_mut_ptr();
2145+
// SAFETY:
2146+
// - `ptr` is guaranteed to be valid for `self.len` elements
2147+
let spare_ptr = unsafe { ptr.add(self.len) };
21452148
let spare_ptr = spare_ptr.cast::<MaybeUninit<T>>();
21462149
let spare_len = self.buf.capacity() - self.len;
21472150

21482151
// SAFETY:
2149-
// - `ptr` is guaranteed to be valid for `len` elements
2152+
// - `ptr` is guaranteed to be valid for `self.len` elements
21502153
// - `spare_ptr` is pointing one element past the buffer, so it doesn't overlap with `initialized`
21512154
unsafe {
21522155
let initialized = slice::from_raw_parts_mut(ptr, self.len);

0 commit comments

Comments
 (0)