Skip to content
Prev Previous commit
Next Next commit
Error on overflow when reserving
  • Loading branch information
ssande7 authored and bluss committed Apr 6, 2024
commit 8e030d1075892236e82ba9d040fbd55f56338a7f
14 changes: 6 additions & 8 deletions src/impl_owned_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,6 @@ impl<A> Array<A, Ix2>
/// This is useful when pushing or appending repeatedly to an array to avoid multiple
/// allocations.
///
/// ***Panics*** if the new capacity would exceed `usize::MAX`.
///
/// ***Errors*** with a shape error if the resultant capacity is larger than the addressable
/// bounds; that is, the product of non-zero axis lengths once `axis` has been extended by
/// `additional` exceeds `isize::MAX`.
Expand All @@ -285,8 +283,6 @@ impl<A> Array<A, Ix2>
/// This is useful when pushing or appending repeatedly to an array to avoid multiple
/// allocations.
///
/// ***Panics*** if the new capacity would exceed `usize::MAX`.
///
/// ***Errors*** with a shape error if the resultant capacity is larger than the addressable
/// bounds; that is, the product of non-zero axis lengths once `axis` has been extended by
/// `additional` exceeds `isize::MAX`.
Expand Down Expand Up @@ -809,7 +805,7 @@ where D: Dimension
/// This is useful when pushing or appending repeatedly to an array to avoid multiple
/// allocations.
///
/// ***Panics*** if the axis is out of bounds or if the new capacity would exceed `usize::MAX`.
/// ***Panics*** if the axis is out of bounds.
///
/// ***Errors*** with a shape error if the resultant capacity is larger than the addressable
/// bounds; that is, the product of non-zero axis lengths once `axis` has been extended by
Expand All @@ -829,9 +825,11 @@ where D: Dimension
let self_dim = self.raw_dim();
let remaining_shape = self_dim.remove_axis(axis);

// Make sure added capacity doesn't overflow
debug_assert!(remaining_shape.size().checked_mul(additional).is_some());
let len_to_append = remaining_shape.size() * additional;
// Make sure added capacity doesn't overflow usize::MAX
let len_to_append = remaining_shape
.size()
.checked_mul(additional)
.ok_or(ShapeError::from_kind(ErrorKind::Overflow))?;

// Make sure new capacity is still in bounds
let mut res_dim = self_dim;
Expand Down