11use crate :: cmp;
2- use crate :: convert:: TryFrom ;
32use crate :: fmt;
43use crate :: mem;
54use crate :: num:: NonZeroUsize ;
@@ -303,18 +302,10 @@ impl Layout {
303302 // > must not overflow isize (i.e., the rounded value must be
304303 // > less than or equal to `isize::MAX`)
305304 let padded_size = self . size ( ) + self . padding_needed_for ( self . align ( ) ) ;
306- // Size manipulation is done in isize space to avoid overflowing isize.
307- let n = isize:: try_from ( n) . map_err ( |_| LayoutError ) ?;
308- let alloc_size = ( padded_size as isize ) . checked_mul ( n) . ok_or ( LayoutError ) ?;
305+ let alloc_size = padded_size. checked_mul ( n) . ok_or ( LayoutError ) ?;
309306
310- // SAFETY: self.align is already known to be valid and alloc_size has been
311- // padded already.
312- unsafe {
313- Ok ( (
314- Layout :: from_size_align_unchecked ( alloc_size as usize , self . align ( ) ) ,
315- padded_size as usize ,
316- ) )
317- }
307+ // The safe constructor is called here to enforce the isize size limit.
308+ Layout :: from_size_align ( alloc_size, self . align ( ) ) . map ( |layout| ( layout, padded_size) )
318309 }
319310
320311 /// Creates a layout describing the record for `self` followed by
@@ -368,12 +359,12 @@ impl Layout {
368359 let new_align = cmp:: max ( self . align ( ) , next. align ( ) ) ;
369360 let pad = self . padding_needed_for ( next. align ( ) ) ;
370361
371- // Size manipulation is done in isize space to avoid overflowing isize.
372- let offset = ( self . size ( ) as isize ) . checked_add ( pad as isize ) . ok_or ( LayoutError ) ?;
373- let new_size = offset. checked_add ( next. size ( ) as isize ) . ok_or ( LayoutError ) ?;
362+ let offset = self . size ( ) . checked_add ( pad) . ok_or ( LayoutError ) ?;
363+ let new_size = offset. checked_add ( next. size ( ) ) . ok_or ( LayoutError ) ?;
374364
375- let layout = Layout :: from_size_align ( new_size as usize , new_align) ?;
376- Ok ( ( layout, offset as usize ) )
365+ // The safe constructor is called here to enforce the isize size limit.
366+ let layout = Layout :: from_size_align ( new_size, new_align) ?;
367+ Ok ( ( layout, offset) )
377368 }
378369
379370 /// Creates a layout describing the record for `n` instances of
@@ -391,9 +382,8 @@ impl Layout {
391382 #[ unstable( feature = "alloc_layout_extra" , issue = "55724" ) ]
392383 #[ inline]
393384 pub fn repeat_packed ( & self , n : usize ) -> Result < Self , LayoutError > {
394- // Size manipulation is done in isize space to avoid overflowing isize.
395- let n = isize:: try_from ( n) . map_err ( |_| LayoutError ) ?;
396- let size = ( self . size ( ) as isize ) . checked_mul ( n) . ok_or ( LayoutError ) ?;
385+ let size = self . size ( ) . checked_mul ( n) . ok_or ( LayoutError ) ?;
386+ // The safe constructor is called here to enforce the isize size limit.
397387 Layout :: from_size_align ( size as usize , self . align ( ) )
398388 }
399389
@@ -406,10 +396,9 @@ impl Layout {
406396 #[ unstable( feature = "alloc_layout_extra" , issue = "55724" ) ]
407397 #[ inline]
408398 pub fn extend_packed ( & self , next : Self ) -> Result < Self , LayoutError > {
409- // Size manipulation is done in isize space to avoid overflowing isize.
410- let new_size =
411- ( self . size ( ) as isize ) . checked_add ( next. size ( ) as isize ) . ok_or ( LayoutError ) ?;
412- Layout :: from_size_align ( new_size as usize , self . align ( ) )
399+ let new_size = self . size ( ) . checked_add ( next. size ( ) ) . ok_or ( LayoutError ) ?;
400+ // The safe constructor is called here to enforce the isize size limit.
401+ Layout :: from_size_align ( new_size, self . align ( ) )
413402 }
414403
415404 /// Creates a layout describing the record for a `[T; n]`.
@@ -418,19 +407,9 @@ impl Layout {
418407 #[ stable( feature = "alloc_layout_manipulation" , since = "1.44.0" ) ]
419408 #[ inline]
420409 pub fn array < T > ( n : usize ) -> Result < Self , LayoutError > {
421- // Size manipulation is done in isize space to avoid overflowing isize.
422- let n = isize:: try_from ( n) . map_err ( |_| LayoutError ) ?;
423- let array_size = ( mem:: size_of :: < T > ( ) as isize ) . checked_mul ( n) . ok_or ( LayoutError ) ?;
424-
425- // SAFETY:
426- // - Size: `array_size` cannot be too big because `size_of::<T>()` must
427- // be a multiple of `align_of::<T>()`. Therefore, `array_size`
428- // rounded up to the nearest multiple of `align_of::<T>()` is just
429- // `array_size`. And `array_size` cannot be too big because it was
430- // just checked by the `checked_mul()`.
431- // - Alignment: `align_of::<T>()` will always give an acceptable
432- // (non-zero, power of two) alignment.
433- Ok ( unsafe { Layout :: from_size_align_unchecked ( array_size as usize , mem:: align_of :: < T > ( ) ) } )
410+ let array_size = mem:: size_of :: < T > ( ) . checked_mul ( n) . ok_or ( LayoutError ) ?;
411+ // The safe constructor is called here to enforce the isize size limit.
412+ Layout :: from_size_align ( array_size, mem:: align_of :: < T > ( ) )
434413 }
435414}
436415
0 commit comments