diff liboctave/array/Sparse.h @ 27081:c0d8ce61c1c9 stable

Always reserve at least 1 element of storage for sparse matrices (bug #56232). * Sparse.h (SparseRep (void), SparseRep (octave_idx_type n), SparseRep (octave_idx_type nr, octave_idx_type nc, octave_idx_type nz = 1)): Rewrite constructors to always create valid d and r pointers with enough memory for 1 value. Always initialize nzmx to at least 1. * Sparse.cc (SparseRep::change_length): Check for nz == 0 and change value to 1 so that sparse array always maintains memory for at least one value. * amd.cc (Famd): Re-write failing BIST test that now works. * data.cc (Fnzmax): Document that nzmax will always return at least 1. * sparse.cc (Fspalloc): Document that 1 element will always be allocated.
author Rik <rik@octave.org>
date Wed, 08 May 2019 15:18:32 -0700
parents 00f796120a6d
children 9a5b6b929f75
line wrap: on
line diff
--- a/liboctave/array/Sparse.h	Mon May 06 20:25:15 2019 -0700
+++ b/liboctave/array/Sparse.h	Wed May 08 15:18:32 2019 -0700
@@ -68,20 +68,22 @@
     octave::refcount<int> count;
 
     SparseRep (void)
-      : d (nullptr), r (nullptr), c (new octave_idx_type [1] {}),
-        nzmx (0), nrows (0), ncols (0), count (1)
+      : d (new T [1]), r (new octave_idx_type [1] {}),
+        c (new octave_idx_type [1] {}),
+        nzmx (1), nrows (0), ncols (0), count (1)
     { }
 
     SparseRep (octave_idx_type n)
-      : d (nullptr), r (nullptr), c (new octave_idx_type [n+1] {}),
-        nzmx (0), nrows (n), ncols (n), count (1)
+      : d (new T [1]), r (new octave_idx_type [1] {}),
+        c (new octave_idx_type [n+1] {}),
+        nzmx (1), nrows (n), ncols (n), count (1)
     { }
 
-    SparseRep (octave_idx_type nr, octave_idx_type nc, octave_idx_type nz = 0)
-      : d (nz > 0 ? new T [nz] : nullptr),
-        r (nz > 0 ? new octave_idx_type [nz] {} : nullptr),
-        c (new octave_idx_type [nc+1] {}), nzmx (nz), nrows (nr),
-        ncols (nc), count (1)
+    SparseRep (octave_idx_type nr, octave_idx_type nc, octave_idx_type nz = 1)
+      : d (nz > 0 ? new T [nz] : new T [1]),
+        r (nz > 0 ? new octave_idx_type [nz] {} : new octave_idx_type [1] {}),
+        c (new octave_idx_type [nc+1] {}),
+        nzmx (nz > 0 ? nz : 1), nrows (nr), ncols (nc), count (1)
     { }
 
     SparseRep (const SparseRep& a)