Skip to content

Commit

Permalink
Check for usize overflow of new capacity
Browse files Browse the repository at this point in the history
  • Loading branch information
ssande7 authored and bluss committed Mar 10, 2024
1 parent ebb3fcf commit eedc8dd
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/impl_owned_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ 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 @@ -195,6 +197,8 @@ 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 @@ -708,7 +712,7 @@ impl<A, D> Array<A, D>
/// This is useful when pushing or appending repeatedly to an array to avoid multiple
/// allocations.
///
/// ***Panics*** if the axis is out of bounds.
/// ***Panics*** if the axis is out of bounds or 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
Expand All @@ -734,7 +738,9 @@ impl<A, D> Array<A, D>
let mut res_dim = self_dim;
res_dim[axis.index()] += additional;
let new_len = dimension::size_of_shape_checked(&res_dim)?;
debug_assert_eq!(self.len() + len_to_append, new_len);

// Check whether len_to_append would cause an overflow
debug_assert_eq!(self.len().checked_add(len_to_append).unwrap(), new_len);

unsafe {
// grow backing storage and update head ptr
Expand Down

0 comments on commit eedc8dd

Please sign in to comment.