diff options
| author | Benoît du Garreau <bdgdlm@outlook.com> | 2021-08-02 11:34:37 +0200 |
|---|---|---|
| committer | Benoît du Garreau <bdgdlm@outlook.com> | 2022-02-12 11:51:15 +0100 |
| commit | d3e2ffcbc69eadc63a7265a7902739080b9338de (patch) | |
| tree | f7c8c4605def5843c5ccde9ca3788e6d7592f9f3 /library/alloc/src | |
| parent | 6027182328c341e672b58c820777941814643a3b (diff) | |
| download | rust-d3e2ffcbc69eadc63a7265a7902739080b9338de.tar.gz rust-d3e2ffcbc69eadc63a7265a7902739080b9338de.zip | |
Fix `shrink` and `capacity_from_bytes`
Diffstat (limited to 'library/alloc/src')
| -rw-r--r-- | library/alloc/src/raw_vec.rs | 5 |
1 files changed, 3 insertions, 2 deletions
diff --git a/library/alloc/src/raw_vec.rs b/library/alloc/src/raw_vec.rs index 0edcaa9ef76..ae1f8c0e513 100644 --- a/library/alloc/src/raw_vec.rs +++ b/library/alloc/src/raw_vec.rs @@ -425,10 +425,11 @@ impl<T, A: Allocator> RawVec<T, A> { assert!(cap <= self.capacity(), "Tried to shrink to a larger capacity"); let (ptr, layout) = if let Some(mem) = self.current_memory() { mem } else { return Ok(()) }; - let new_size = cap * mem::size_of::<T>(); let ptr = unsafe { - let new_layout = Layout::from_size_align_unchecked(new_size, layout.align()); + // `Layout::array` cannot overflow here because it would have + // owerflown earlier when capacity was larger. + let new_layout = Layout::array::<T>(cap).unwrap_unchecked(); self.alloc .shrink(ptr, layout, new_layout) .map_err(|_| AllocError { layout: new_layout, non_exhaustive: () })? |
