Skip to content

Commit d695430

Browse files
committed
Check for usize overflow of new capacity
1 parent 7b2d2ab commit d695430

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
@@ -172,6 +172,8 @@ impl<A> Array<A, Ix2> {
172172
/// This is useful when pushing or appending repeatedly to an array to avoid multiple
173173
/// allocations.
174174
///
175+
/// ***Panics*** if the new capacity would exceed `usize::MAX`.
176+
///
175177
/// ***Errors*** with a shape error if the resultant capacity is larger than the addressable
176178
/// bounds; that is, the product of non-zero axis lengths once `axis` has been extended by
177179
/// `additional` exceeds `isize::MAX`.
@@ -194,6 +196,8 @@ impl<A> Array<A, Ix2> {
194196
/// This is useful when pushing or appending repeatedly to an array to avoid multiple
195197
/// allocations.
196198
///
199+
/// ***Panics*** if the new capacity would exceed `usize::MAX`.
200+
///
197201
/// ***Errors*** with a shape error if the resultant capacity is larger than the addressable
198202
/// bounds; that is, the product of non-zero axis lengths once `axis` has been extended by
199203
/// `additional` exceeds `isize::MAX`.
@@ -707,7 +711,7 @@ impl<A, D> Array<A, D>
707711
/// This is useful when pushing or appending repeatedly to an array to avoid multiple
708712
/// allocations.
709713
///
710-
/// ***Panics*** if the axis is out of bounds.
714+
/// ***Panics*** if the axis is out of bounds or if the new capacity would exceed `usize::MAX`.
711715
///
712716
/// ***Errors*** with a shape error if the resultant capacity is larger than the addressable
713717
/// bounds; that is, the product of non-zero axis lengths once `axis` has been extended by
@@ -733,7 +737,9 @@ impl<A, D> Array<A, D>
733737
let mut res_dim = self_dim;
734738
res_dim[axis.index()] += additional;
735739
let new_len = dimension::size_of_shape_checked(&res_dim)?;
736-
debug_assert_eq!(self.len() + len_to_append, new_len);
740+
741+
// Check whether len_to_append would cause an overflow
742+
debug_assert_eq!(self.len().checked_add(len_to_append).unwrap(), new_len);
737743

738744
unsafe {
739745
// grow backing storage and update head ptr

0 commit comments

Comments
 (0)