comparison 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
comparison
equal deleted inserted replaced
17663:7975d75f933c 17664:f4b0430fa5fd
299 nc = c.extent (0); 299 nc = c.extent (0);
300 else if (c.extent (nc) > nc) 300 else if (c.extent (nc) > nc)
301 (*current_liboctave_error_handler) 301 (*current_liboctave_error_handler)
302 ("sparse: column index %d out of bound %d", r.extent (nc), nc); 302 ("sparse: column index %d out of bound %d", r.extent (nc), nc);
303 303
304 rep = new typename Sparse<T>::SparseRep (nr, nc, (nzm > 0 ? nzm : 0));
305
306 dimensions = dim_vector (nr, nc); 304 dimensions = dim_vector (nr, nc);
307 305
308 octave_idx_type n = a.numel (), rl = r.length (nr), cl = c.length (nc); 306 octave_idx_type n = a.numel (), rl = r.length (nr), cl = c.length (nc);
309 bool a_scalar = n == 1; 307 bool a_scalar = n == 1;
310 if (a_scalar) 308 if (a_scalar)
315 n = cl; 313 n = cl;
316 } 314 }
317 315
318 if ((rl != 1 && rl != n) || (cl != 1 && cl != n)) 316 if ((rl != 1 && rl != n) || (cl != 1 && cl != n))
319 (*current_liboctave_error_handler) ("sparse: dimension mismatch"); 317 (*current_liboctave_error_handler) ("sparse: dimension mismatch");
318
319 // Only create rep after input validation to avoid memory leak.
320 rep = new typename Sparse<T>::SparseRep (nr, nc, (nzm > 0 ? nzm : 0));
320 321
321 if (rl <= 1 && cl <= 1) 322 if (rl <= 1 && cl <= 1)
322 { 323 {
323 if (n == 1 && a(0) != T ()) 324 if (n == 1 && a(0) != T ())
324 { 325 {