Skip to content

Commit 8e030d1

Browse files
ssande7bluss
authored andcommitted
Error on overflow when reserving
1 parent 4b022eb commit 8e030d1

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

src/impl_owned_array.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,6 @@ impl<A> Array<A, Ix2>
260260
/// This is useful when pushing or appending repeatedly to an array to avoid multiple
261261
/// allocations.
262262
///
263-
/// ***Panics*** if the new capacity would exceed `usize::MAX`.
264-
///
265263
/// ***Errors*** with a shape error if the resultant capacity is larger than the addressable
266264
/// bounds; that is, the product of non-zero axis lengths once `axis` has been extended by
267265
/// `additional` exceeds `isize::MAX`.
@@ -285,8 +283,6 @@ impl<A> Array<A, Ix2>
285283
/// This is useful when pushing or appending repeatedly to an array to avoid multiple
286284
/// allocations.
287285
///
288-
/// ***Panics*** if the new capacity would exceed `usize::MAX`.
289-
///
290286
/// ***Errors*** with a shape error if the resultant capacity is larger than the addressable
291287
/// bounds; that is, the product of non-zero axis lengths once `axis` has been extended by
292288
/// `additional` exceeds `isize::MAX`.
@@ -809,7 +805,7 @@ where D: Dimension
809805
/// This is useful when pushing or appending repeatedly to an array to avoid multiple
810806
/// allocations.
811807
///
812-
/// ***Panics*** if the axis is out of bounds or if the new capacity would exceed `usize::MAX`.
808+
/// ***Panics*** if the axis is out of bounds.
813809
///
814810
/// ***Errors*** with a shape error if the resultant capacity is larger than the addressable
815811
/// bounds; that is, the product of non-zero axis lengths once `axis` has been extended by
@@ -829,9 +825,11 @@ where D: Dimension
829825
let self_dim = self.raw_dim();
830826
let remaining_shape = self_dim.remove_axis(axis);
831827

832-
// Make sure added capacity doesn't overflow
833-
debug_assert!(remaining_shape.size().checked_mul(additional).is_some());
834-
let len_to_append = remaining_shape.size() * additional;
828+
// Make sure added capacity doesn't overflow usize::MAX
829+
let len_to_append = remaining_shape
830+
.size()
831+
.checked_mul(additional)
832+
.ok_or(ShapeError::from_kind(ErrorKind::Overflow))?;
835833

836834
// Make sure new capacity is still in bounds
837835
let mut res_dim = self_dim;

0 commit comments

Comments
 (0)