changeset 17664:f4b0430fa5fd

Avoid memory leak in Sparse constructor. * liboctave/array/Sparse.cc: Move call to new() in constructor after all input validation in case validation fails and error() called without freeing memory.
author Rik <rik@octave.org>
date Tue, 15 Oct 2013 16:33:30 -0700
parents 7975d75f933c
children 78e9bfdc544e
files liboctave/array/Sparse.cc
diffstat 1 files changed, 3 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/liboctave/array/Sparse.cc	Tue Oct 15 14:59:09 2013 -0700
+++ b/liboctave/array/Sparse.cc	Tue Oct 15 16:33:30 2013 -0700
@@ -301,8 +301,6 @@
     (*current_liboctave_error_handler)
       ("sparse: column index %d out of bound %d", r.extent (nc), nc);
 
-  rep = new typename Sparse<T>::SparseRep (nr, nc, (nzm > 0 ? nzm : 0));
-
   dimensions = dim_vector (nr, nc);
 
   octave_idx_type n = a.numel (), rl = r.length (nr), cl = c.length (nc);
@@ -318,6 +316,9 @@
   if ((rl != 1 && rl != n) || (cl != 1 && cl != n))
     (*current_liboctave_error_handler) ("sparse: dimension mismatch");
 
+  // Only create rep after input validation to avoid memory leak.
+  rep = new typename Sparse<T>::SparseRep (nr, nc, (nzm > 0 ? nzm : 0));
+
   if (rl <= 1 && cl <= 1)
     {
       if (n == 1 && a(0) != T ())