File tree Expand file tree Collapse file tree 1 file changed +12
-2
lines changed Expand file tree Collapse file tree 1 file changed +12
-2
lines changed Original file line number Diff line number Diff line change @@ -267,10 +267,20 @@ where
267267 {
268268 let alloc = Global ;
269269 unsafe {
270- let new_layout = Layout :: array :: < T > ( dst_cap) . unwrap ( ) ;
270+ // The old allocation exists, therefore it must have a valid layout.
271+ let src_align = mem:: align_of :: < I :: Src > ( ) ;
272+ let src_size = mem:: size_of :: < I :: Src > ( ) . unchecked_mul ( src_cap) ;
273+ let old_layout = Layout :: from_size_align_unchecked ( src_size, src_align) ;
274+
275+ // The must be equal or smaller for in-place iteration to be possible
276+ // therefore the new layout must be ≤ the old one and therefore valid.
277+ let dst_align = mem:: align_of :: < T > ( ) ;
278+ let dst_size = mem:: size_of :: < T > ( ) . unchecked_mul ( dst_cap) ;
279+ let new_layout = Layout :: from_size_align_unchecked ( dst_size, dst_align) ;
280+
271281 let result = alloc. shrink (
272282 NonNull :: new_unchecked ( dst_buf as * mut u8 ) ,
273- Layout :: array :: < I :: Src > ( src_cap ) . unwrap ( ) ,
283+ old_layout ,
274284 new_layout,
275285 ) ;
276286 let Ok ( reallocated) = result else { handle_alloc_error ( new_layout) } ;
You can’t perform that action at this time.
0 commit comments