changeset 32045:f18da620ab4d stable

Fix floating point exception when Sparse array reshaped to 0x0 (bug #64080) * Sparse.cc (reshape): Test for empty matrix (number of rows or columns equal to 0) and immediately return constructed, but unfilled, empty sparse matrix. * data.cc (Freshape): Add BIST test for bug #64080.
author Rik <rik@octave.org>
date Thu, 20 Apr 2023 18:58:57 -0700
parents 1824e0ee4088
children 39700c1ea93e 88e7edd94e2e
files libinterp/corefcn/data.cc liboctave/array/Sparse.cc
diffstat 2 files changed, 5 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/corefcn/data.cc	Tue Apr 18 11:09:17 2023 -0700
+++ b/libinterp/corefcn/data.cc	Thu Apr 20 18:58:57 2023 -0700
@@ -5981,6 +5981,8 @@
 %!assert (size (reshape (ones (15, 4, "single"), 1, 60)), [1, 60])
 %!assert (size (reshape (ones (15, 4, "single"), 60, 1)), [60, 1])
 
+%!assert <*64080> (size (reshape (sparse (0, 1), 0, 0)), [0, 0])
+
 %!test
 %! s.a = 1;
 %! fail ("reshape (s, 2, 3)", "can't reshape 1x1 array to 2x3 array");
--- a/liboctave/array/Sparse.cc	Tue Apr 18 11:09:17 2023 -0700
+++ b/liboctave/array/Sparse.cc	Thu Apr 20 18:58:57 2023 -0700
@@ -873,6 +873,9 @@
           octave_idx_type old_nr = rows ();
           octave_idx_type old_nc = cols ();
           retval = Sparse<T, Alloc> (new_nr, new_nc, new_nnz);
+          // Special case for empty matrices (bug #64080)
+          if (new_nr == 0 || new_nc == 0)
+            return retval;
 
           octave_idx_type kk = 0;
           retval.xcidx (0) = 0;