diff --git a/src/lib.rs b/src/lib.rs index ae5786d..af72fc5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -436,16 +436,18 @@ fn alloc_align() -> usize { /// /// # Panics /// -/// Panics if the required size overflows `isize::MAX`. +/// Panics if the required size overflows `isize::MAX` when rounded up to the required alignment. fn layout(cap: usize) -> Layout { - unsafe { Layout::from_size_align_unchecked(alloc_size::(cap), alloc_align::()) } + Layout::from_size_align(alloc_size::(cap), alloc_align::()) + .ok() + .unwrap_cap_overflow() } /// Allocates a header (and array) for a `ThinVec` with the given capacity. /// /// # Panics /// -/// Panics if the required size overflows `isize::MAX`. +/// Panics if the required size overflows `isize::MAX` when rounded up to the required alignment. fn header_with_capacity(cap: usize, is_auto: bool) -> NonNull
{ debug_assert!(cap > 0); unsafe { @@ -3104,6 +3106,12 @@ mod tests { ThinVec::::new(); } + #[test] + #[should_panic] + fn test_cap_plus_header_rounded_up_overflows() { + let _ = ThinVec::::with_capacity(isize::MAX as usize - size_of::()); + } + #[test] fn test_data_ptr_alignment() { let v = ThinVec::::new();