Skip to content

Commit 86c1c43

Browse files
committed
fix pointer invalidation when extnding a vector from an untrusted iterator
1 parent 6556549 commit 86c1c43

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

src/liballoc/vec.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2019,6 +2019,8 @@ where
20192019
let (lower, _) = iterator.size_hint();
20202020
let mut vector = Vec::with_capacity(lower.saturating_add(1));
20212021
unsafe {
2022+
// `vector` is new, cannot have aliases, so us getting exclusive references
2023+
// here is okay.
20222024
ptr::write(vector.get_unchecked_mut(0), element);
20232025
vector.set_len(1);
20242026
}
@@ -2145,7 +2147,7 @@ impl<T> Vec<T> {
21452147
self.reserve(lower.saturating_add(1));
21462148
}
21472149
unsafe {
2148-
ptr::write(self.get_unchecked_mut(len), element);
2150+
ptr::write(self.as_mut_ptr().add(len), element);
21492151
// NB can't overflow since we would have had to alloc the address space
21502152
self.set_len(len + 1);
21512153
}

0 commit comments

Comments
 (0)