changeset 27323:1582a6140275 stable

Stop segfault when calling 3-input form of diag with cell arrays (bug #56711). * data.cc (Fdiag): Add BIST test for bug #56711. * Array.cc (Array<T>::diag): Determine maximum number of elements for new diag cell array as the minimum of the number of elements in the vector, the number of rows, or the number of columns. Use this maximum in for loop which copies elements in to output.
author Rik <rik@octave.org>
date Mon, 05 Aug 2019 17:15:57 -0700
parents da1d653570a3
children 0cf70e494a3f 29e52bcc72af
files libinterp/corefcn/data.cc liboctave/array/Array.cc
diffstat 2 files changed, 3 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/corefcn/data.cc	Mon Aug 05 08:28:01 2019 -0700
+++ b/libinterp/corefcn/data.cc	Mon Aug 05 17:15:57 2019 -0700
@@ -1283,6 +1283,7 @@
 %!assert (diag (1, 2, 3), [1,0,0; 0,0,0])
 %!assert (diag ({1}, 2, 3), {1,[],[]; [],[],[]})
 %!assert (diag ({1,2}, 3, 4), {1,[],[],[]; [],2,[],[]; [],[],[],[]})
+%!assert <*56711> (diag ({1,2,3}, 2, 1), {1; []})
 
 ## Test out-of-range diagonals
 %!assert (diag (ones (3,3), 4), zeros (0, 1))
--- a/liboctave/array/Array.cc	Mon Aug 05 08:28:01 2019 -0700
+++ b/liboctave/array/Array.cc	Mon Aug 05 17:15:57 2019 -0700
@@ -2617,7 +2617,8 @@
 
   Array<T> retval (dim_vector (m, n), resize_fill_value ());
 
-  for (octave_idx_type i = 0; i < numel (); i++)
+  octave_idx_type nel = std::min (numel (), std::min (m, n));
+  for (octave_idx_type i = 0; i < nel; i++)
     retval.xelem (i, i) = xelem (i);
 
   return retval;