From c1fa02331ad60e73569f8351401f183089ff89bf Mon Sep 17 00:00:00 2001 From: Tim Diekmann Date: Wed, 25 Mar 2020 18:36:03 +0100 Subject: [PATCH] Fix ZST handling for `RawVec` --- src/liballoc/raw_vec.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/liballoc/raw_vec.rs b/src/liballoc/raw_vec.rs index aee2367bd95..a51d30448d1 100644 --- a/src/liballoc/raw_vec.rs +++ b/src/liballoc/raw_vec.rs @@ -483,14 +483,14 @@ impl RawVec { placement: ReallocPlacement, init: AllocInit, ) -> Result<(), TryReserveError> { + let elem_size = mem::size_of::(); + if elem_size == 0 { + // Since we return a capacity of `usize::MAX` when `elem_size` is + // 0, getting to here necessarily means the `RawVec` is overfull. + return Err(CapacityOverflow); + } let layout = match strategy { Double => unsafe { - let elem_size = mem::size_of::(); - if elem_size == 0 { - // Since we return a capacity of `usize::MAX` when `elem_size` is - // 0, getting to here necessarily means the `RawVec` is overfull. - return Err(CapacityOverflow); - } // Since we guarantee that we never allocate more than `isize::MAX` bytes, // `elem_size * self.cap <= isize::MAX` as a precondition, so this can't overflow. // Additionally the alignment will never be too large as to "not be satisfiable",