Mercurial > hg > octave-nkf
diff liboctave/array/Sparse.cc @ 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 | 7ed397c8ca68 |
children | d63878346099 |
line wrap: on
line diff
--- a/liboctave/array/Sparse.cc +++ b/liboctave/array/Sparse.cc @@ -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 ())