# HG changeset patch # User Rik # Date 1381880010 25200 # Node ID f4b0430fa5fd951e5f3f56e4691409e5b1426710 # Parent 7975d75f933ce1cdda0545d55b314b33c29d144a 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. diff -r 7975d75f933c -r f4b0430fa5fd liboctave/array/Sparse.cc --- 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::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::SparseRep (nr, nc, (nzm > 0 ? nzm : 0)); + if (rl <= 1 && cl <= 1) { if (n == 1 && a(0) != T ())