From ac55a4cb4a4a60cbe5f97d28d241f10bfeab637e Mon Sep 17 00:00:00 2001 From: Guilliam Xavier Date: Thu, 31 Aug 2023 10:16:06 +0200 Subject: [PATCH] Fix `Vec::push_all` ptr code in exception-safety --- src/exception-safety.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/exception-safety.md b/src/exception-safety.md index 8404bb85..09d1b3da 100644 --- a/src/exception-safety.md +++ b/src/exception-safety.md @@ -41,11 +41,13 @@ impl Vec { fn push_all(&mut self, to_push: &[T]) { self.reserve(to_push.len()); unsafe { + let end_ptr = self.as_mut_ptr().add(self.len()); + // can't overflow because we just reserved this self.set_len(self.len() + to_push.len()); for (i, x) in to_push.iter().enumerate() { - self.ptr().add(i).write(x.clone()); + end_ptr.add(i).write(x.clone()); } } }