changeset 18964:ac0c04e4d141 stable

Fix segfault with sparse inputs to complex() (bug #42290). * data.cc (Fcomplex): Correctly call SparseComplexMatrix constructor with nrows, ncols, initial_value. Use in-place operator += to clarify code.
author Rik <rik@octave.org>
date Fri, 23 May 2014 11:48:23 -0700
parents 4293f49795d9
children d99475e26c78
files libinterp/corefcn/data.cc
diffstat 1 files changed, 5 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/corefcn/data.cc
+++ b/libinterp/corefcn/data.cc
@@ -3121,17 +3121,16 @@
                     result = Complex (0, 1) * SparseComplexMatrix (im_val);
                   else
                     {
-                      result = SparseComplexMatrix (im_val.dims (), re_val (0));
                       octave_idx_type nr = im_val.rows ();
                       octave_idx_type nc = im_val.cols ();
+                      result = SparseComplexMatrix (nr, nc, re_val(0));
 
                       for (octave_idx_type j = 0; j < nc; j++)
                         {
                           octave_idx_type off = j * nr;
                           for (octave_idx_type i = im_val.cidx (j);
                                i < im_val.cidx (j + 1); i++)
-                            result.data (im_val.ridx (i) + off) =
-                              result.data (im_val.ridx (i) + off) +
+                            result.data (im_val.ridx (i) + off) +=
                               Complex (0, im_val.data (i));
                         }
                     }
@@ -3144,19 +3143,17 @@
                     result = SparseComplexMatrix (re_val);
                   else
                     {
-                      result = SparseComplexMatrix (re_val.rows (),
-                                                    re_val.cols (),
-                                                    Complex (0, im_val (0)));
                       octave_idx_type nr = re_val.rows ();
                       octave_idx_type nc = re_val.cols ();
+                      result = SparseComplexMatrix (nr, nc,
+                                                    Complex (0, im_val(0)));
 
                       for (octave_idx_type j = 0; j < nc; j++)
                         {
                           octave_idx_type off = j * nr;
                           for (octave_idx_type i = re_val.cidx (j);
                                i < re_val.cidx (j + 1); i++)
-                            result.data (re_val.ridx (i) + off) =
-                              result.data (re_val.ridx (i) + off) +
+                            result.data (re_val.ridx (i) + off) +=
                               re_val.data (i);
                         }
                     }