Skip to content

Commit bbeed89

Browse files
committed
Add missing checks when converting slices to views
1 parent 1a7c020 commit bbeed89

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

src/arraytraits.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -325,8 +325,9 @@ where
325325

326326
/// Implementation of ArrayView2::from(&S) where S is a slice to a 2D array
327327
///
328-
/// **Panics** if the product of non-zero axis lengths overflows `isize` (This can only occur if A
329-
/// is zero-sized because slices cannot contain more than `isize::MAX` number of bytes).
328+
/// **Panics** if the product of non-zero axis lengths overflows `isize`. (This
329+
/// can only occur if A is zero-sized or if `N` is zero, because slices cannot
330+
/// contain more than `isize::MAX` number of bytes.)
330331
impl<'a, A, const N: usize> From<&'a [[A; N]]> for ArrayView<'a, A, Ix2> {
331332
/// Create a two-dimensional read-only array view of the data in `slice`
332333
fn from(xs: &'a [[A; N]]) -> Self {
@@ -336,6 +337,11 @@ impl<'a, A, const N: usize> From<&'a [[A; N]]> for ArrayView<'a, A, Ix2> {
336337
if size_of::<A>() == 0 {
337338
dimension::size_of_shape_checked(&dim)
338339
.expect("Product of non-zero axis lengths must not overflow isize.");
340+
} else if N == 0 {
341+
assert!(
342+
xs.len() <= isize::MAX as usize,
343+
"Product of non-zero axis lengths must not overflow isize.",
344+
);
339345
}
340346

341347
// `cols * rows` is guaranteed to fit in `isize` because we checked that it fits in
@@ -381,8 +387,9 @@ where
381387

382388
/// Implementation of ArrayViewMut2::from(&S) where S is a slice to a 2D array
383389
///
384-
/// **Panics** if the product of non-zero axis lengths overflows `isize` (This can only occur if A
385-
/// is zero-sized because slices cannot contain more than `isize::MAX` number of bytes).
390+
/// **Panics** if the product of non-zero axis lengths overflows `isize`. (This
391+
/// can only occur if `A` is zero-sized or if `N` is zero, because slices
392+
/// cannot contain more than `isize::MAX` number of bytes.)
386393
impl<'a, A, const N: usize> From<&'a mut [[A; N]]> for ArrayViewMut<'a, A, Ix2> {
387394
/// Create a two-dimensional read-write array view of the data in `slice`
388395
fn from(xs: &'a mut [[A; N]]) -> Self {
@@ -392,6 +399,11 @@ impl<'a, A, const N: usize> From<&'a mut [[A; N]]> for ArrayViewMut<'a, A, Ix2>
392399
if size_of::<A>() == 0 {
393400
dimension::size_of_shape_checked(&dim)
394401
.expect("Product of non-zero axis lengths must not overflow isize.");
402+
} else if N == 0 {
403+
assert!(
404+
xs.len() <= isize::MAX as usize,
405+
"Product of non-zero axis lengths must not overflow isize.",
406+
);
395407
}
396408

397409
// `cols * rows` is guaranteed to fit in `isize` because we checked that it fits in

0 commit comments

Comments
 (0)