In the function `QuadTree_new_from_point_list` in lib/sparse/QuadTree.c,
memory is allocated for tree variables. The check that follows returns
NULL if one of the pointers is NULL (indicating memory allocation
failure). However, it is possible that memory allocation succeeds for one
or two variables, but fails for the third. If this happens, the functions
returns NULL, but the allocated memory isn't freed.
The fix is to free all three pointers in case of a failure. If memory is
allocated it is freed. If the pointer is NULL, free can still safely be
called.
xmin = MALLOC(sizeof(real)*dim);
xmax = MALLOC(sizeof(real)*dim);
center = MALLOC(sizeof(real)*dim);
- if (!xmin || !xmax || !center) return NULL;
+ if (!xmin || !xmax || !center) {
+ FREE(xmin);
+ FREE(xmax);
+ FREE(center);
+ return NULL;
+ }
for (i = 0; i < dim; i++) xmin[i] = coord[i];
for (i = 0; i < dim; i++) xmax[i] = coord[i];