From bbeed89b77e53a3c2ee07c7628c363d8754fe3f1 Mon Sep 17 00:00:00 2001 From: Jim Turner Date: Tue, 7 Dec 2021 00:01:33 -0500 Subject: [PATCH] Add missing checks when converting slices to views --- src/arraytraits.rs | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/arraytraits.rs b/src/arraytraits.rs index 6a4fd1137..db59add20 100644 --- a/src/arraytraits.rs +++ b/src/arraytraits.rs @@ -325,8 +325,9 @@ where /// Implementation of ArrayView2::from(&S) where S is a slice to a 2D array /// -/// **Panics** if the product of non-zero axis lengths overflows `isize` (This can only occur if A -/// is zero-sized because slices cannot contain more than `isize::MAX` number of bytes). +/// **Panics** if the product of non-zero axis lengths overflows `isize`. (This +/// can only occur if A is zero-sized or if `N` is zero, because slices cannot +/// contain more than `isize::MAX` number of bytes.) impl<'a, A, const N: usize> From<&'a [[A; N]]> for ArrayView<'a, A, Ix2> { /// Create a two-dimensional read-only array view of the data in `slice` 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> { if size_of::() == 0 { dimension::size_of_shape_checked(&dim) .expect("Product of non-zero axis lengths must not overflow isize."); + } else if N == 0 { + assert!( + xs.len() <= isize::MAX as usize, + "Product of non-zero axis lengths must not overflow isize.", + ); } // `cols * rows` is guaranteed to fit in `isize` because we checked that it fits in @@ -381,8 +387,9 @@ where /// Implementation of ArrayViewMut2::from(&S) where S is a slice to a 2D array /// -/// **Panics** if the product of non-zero axis lengths overflows `isize` (This can only occur if A -/// is zero-sized because slices cannot contain more than `isize::MAX` number of bytes). +/// **Panics** if the product of non-zero axis lengths overflows `isize`. (This +/// can only occur if `A` is zero-sized or if `N` is zero, because slices +/// cannot contain more than `isize::MAX` number of bytes.) impl<'a, A, const N: usize> From<&'a mut [[A; N]]> for ArrayViewMut<'a, A, Ix2> { /// Create a two-dimensional read-write array view of the data in `slice` 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> if size_of::() == 0 { dimension::size_of_shape_checked(&dim) .expect("Product of non-zero axis lengths must not overflow isize."); + } else if N == 0 { + assert!( + xs.len() <= isize::MAX as usize, + "Product of non-zero axis lengths must not overflow isize.", + ); } // `cols * rows` is guaranteed to fit in `isize` because we checked that it fits in