From b91eecebcdf30c9655ccf4549f6ea643d0eb5330 Mon Sep 17 00:00:00 2001 From: Max Verevkin Date: Sun, 19 Sep 2021 17:00:49 +0300 Subject: [PATCH] Matrix::perp() fix UB --- src/base/matrix.rs | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/src/base/matrix.rs b/src/base/matrix.rs index 13413dbf..0b44cf68 100644 --- a/src/base/matrix.rs +++ b/src/base/matrix.rs @@ -2036,16 +2036,26 @@ impl + SameNumberOfColumns, { - assert!( - self.shape() == (2, 1), - "2D perpendicular product requires (2, 1) vector but found {:?}", - self.shape() + let shape = self.shape(); + assert_eq!( + shape, + b.shape(), + "2D vector perpendicular product dimension mismatch." + ); + assert_eq!( + shape, + (2, 1), + "2D perpendicular product requires (2, 1) vectors {:?}", + shape ); - unsafe { - self.get_unchecked((0, 0)).clone() * b.get_unchecked((1, 0)).clone() - - self.get_unchecked((1, 0)).clone() * b.get_unchecked((0, 0)).clone() - } + // SAFETY: assertion above ensures correct shape + let ax = unsafe { self.get_unchecked((0, 0)).clone() }; + let ay = unsafe { self.get_unchecked((1, 0)).clone() }; + let bx = unsafe { b.get_unchecked((0, 0)).clone() }; + let by = unsafe { b.get_unchecked((1, 0)).clone() }; + + ax * by - ay * bx } // TODO: use specialization instead of an assertion. @@ -2066,17 +2076,14 @@ impl::from_usize(3); - let ncols = SameShapeC::::from_usize(1); - let mut res = Matrix::uninit(nrows, ncols); + let mut res = Matrix::uninit(Dim::from_usize(3), Dim::from_usize(1)); let ax = self.get_unchecked((0, 0)); let ay = self.get_unchecked((1, 0)); @@ -2098,10 +2105,7 @@ impl::from_usize(1); - let ncols = SameShapeC::::from_usize(3); - let mut res = Matrix::uninit(nrows, ncols); + let mut res = Matrix::uninit(Dim::from_usize(1), Dim::from_usize(3)); let ax = self.get_unchecked((0, 0)); let ay = self.get_unchecked((0, 1));