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",