changeset 27083:23761e83756f

Always reserve memory for 1-element in sparse mxArray matrices (bug #56232). * mex.cc (mxArray_sparse (mxClassID, mwSize, mwSize, mwSize, mxComplexity)): Change constructor from mem-init style to actual function. Set nzmax to be at least 1. Use new nzmax in rest of constructor. * mex.cc (set_nzmax): Check input nzmax_arg and make it at least 1 before assigning to nzmax.
author Rik <rik@octave.org>
date Wed, 08 May 2019 18:28:05 -0700
parents 9a5b6b929f75
children df9e3cb79379
files libinterp/corefcn/mex.cc
diffstat 1 files changed, 14 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/corefcn/mex.cc	Wed May 08 15:27:35 2019 -0700
+++ b/libinterp/corefcn/mex.cc	Wed May 08 18:28:05 2019 -0700
@@ -1491,12 +1491,15 @@
 
   mxArray_sparse (mxClassID id_arg, mwSize m, mwSize n, mwSize nzmax_arg,
                   mxComplexity flag = mxREAL)
-    : mxArray_matlab (id_arg, m, n), nzmax (nzmax_arg),
-      pr (mxArray::calloc (nzmax, get_element_size ())),
-      pi (flag == mxCOMPLEX ? mxArray::calloc (nzmax, get_element_size ()) : nullptr),
-      ir (static_cast<mwIndex *> (mxArray::calloc (nzmax, sizeof (mwIndex)))),
-      jc (static_cast<mwIndex *> (mxArray::calloc (n + 1, sizeof (mwIndex))))
-  { }
+    : mxArray_matlab (id_arg, m, n)
+  {
+    nzmax = (nzmax_arg > 0 ? nzmax_arg : 1);
+    pr = mxArray::calloc (nzmax, get_element_size ());
+    pi = (flag == mxCOMPLEX ? mxArray::calloc (nzmax, get_element_size ())
+                            : nullptr);
+    ir = (static_cast<mwIndex *> (mxArray::calloc (nzmax, sizeof (mwIndex))));
+    jc = (static_cast<mwIndex *> (mxArray::calloc (n + 1, sizeof (mwIndex))));
+  }
 
 private:
 
@@ -1561,7 +1564,11 @@
 
   void set_jc (mwIndex *jc_arg) { jc = jc_arg; }
 
-  void set_nzmax (mwSize nzmax_arg) { nzmax = nzmax_arg; }
+  void set_nzmax (mwSize nzmax_arg)
+  {
+    /* Require storage for at least 1 element */
+    nzmax = (nzmax_arg > 0 ? nzmax_arg : 1);
+  }
 
   octave_value as_octave_value (void) const
   {