Skip to content

Commit beb1439

Browse files
ssande7bluss
authored andcommitted
Check for usize overflow of new capacity
1 parent a16707a commit beb1439

1 file changed

Lines changed: 8 additions & 2 deletions

File tree

src/impl_owned_array.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,8 @@ 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+
///
263265
/// ***Errors*** with a shape error if the resultant capacity is larger than the addressable
264266
/// bounds; that is, the product of non-zero axis lengths once `axis` has been extended by
265267
/// `additional` exceeds `isize::MAX`.
@@ -283,6 +285,8 @@ impl<A> Array<A, Ix2>
283285
/// This is useful when pushing or appending repeatedly to an array to avoid multiple
284286
/// allocations.
285287
///
288+
/// ***Panics*** if the new capacity would exceed `usize::MAX`.
289+
///
286290
/// ***Errors*** with a shape error if the resultant capacity is larger than the addressable
287291
/// bounds; that is, the product of non-zero axis lengths once `axis` has been extended by
288292
/// `additional` exceeds `isize::MAX`.
@@ -805,7 +809,7 @@ where D: Dimension
805809
/// This is useful when pushing or appending repeatedly to an array to avoid multiple
806810
/// allocations.
807811
///
808-
/// ***Panics*** if the axis is out of bounds.
812+
/// ***Panics*** if the axis is out of bounds or if the new capacity would exceed `usize::MAX`.
809813
///
810814
/// ***Errors*** with a shape error if the resultant capacity is larger than the addressable
811815
/// bounds; that is, the product of non-zero axis lengths once `axis` has been extended by
@@ -830,7 +834,9 @@ where D: Dimension
830834
let mut res_dim = self_dim;
831835
res_dim[axis.index()] += additional;
832836
let new_len = dimension::size_of_shape_checked(&res_dim)?;
833-
debug_assert_eq!(self.len() + len_to_append, new_len);
837+
838+
// Check whether len_to_append would cause an overflow
839+
debug_assert_eq!(self.len().checked_add(len_to_append).unwrap(), new_len);
834840

835841
unsafe {
836842
// grow backing storage and update head ptr

0 commit comments

Comments
 (0)