From 0191b93db9f56fa58f520f1f5c5f8437edf836c1 Mon Sep 17 00:00:00 2001 From: Kornel Date: Fri, 20 Oct 2023 03:29:46 +0100 Subject: [PATCH] Update library/alloc/src/raw_vec.rs Co-authored-by: scottmcm --- library/alloc/src/raw_vec.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/library/alloc/src/raw_vec.rs b/library/alloc/src/raw_vec.rs index bc994694ad7e..d335ac80d189 100644 --- a/library/alloc/src/raw_vec.rs +++ b/library/alloc/src/raw_vec.rs @@ -291,10 +291,14 @@ impl RawVec { if self.needs_to_grow(len, additional) { do_reserve_and_handle(self, len, additional); - unsafe { - // Inform the optimizer that the reservation has succeeded - core::intrinsics::assume(!self.needs_to_grow(len, additional)); - } + } + + // SAFETY: The call to `do_reserve_and_handle` ensured this + // (or it panicked) and thus the addition cannot overflow. + unsafe { + // Inform the optimizer that the reservation has succeeded + let min_cap = len.unchecked_add(additional); + core::intrinsics::assume(self.cap >= min_cap); } }