Skip to content

Commit 2f04a79

Browse files
committed
Revert Vec::spare_capacity_mut impl to prevent pointers invalidation
1 parent cecdb18 commit 2f04a79

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

library/alloc/src/vec/mod.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1879,7 +1879,15 @@ impl<T, A: Allocator> Vec<T, A> {
18791879
#[unstable(feature = "vec_spare_capacity", issue = "75017")]
18801880
#[inline]
18811881
pub fn spare_capacity_mut(&mut self) -> &mut [MaybeUninit<T>] {
1882-
self.split_at_spare_mut().1
1882+
// Note:
1883+
// This method is not implemented in terms of `split_at_spare_mut`,
1884+
// to prevent invalidation of pointers to the buffer.
1885+
unsafe {
1886+
slice::from_raw_parts_mut(
1887+
self.as_mut_ptr().add(self.len) as *mut MaybeUninit<T>,
1888+
self.buf.capacity() - self.len,
1889+
)
1890+
}
18831891
}
18841892

18851893
/// Returns vector content as a slice of `T`, along with the remaining spare

0 commit comments

Comments
 (0)