From: Erwin Janssen Date: Wed, 7 Dec 2016 13:56:39 +0000 (+0100) Subject: Fixed 3 potential memory leaks in QuadTree.c X-Git-Tag: 2.42.0~229^2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=83c25e485a2cb24753ff429dbe80dcd462881798;p=graphviz Fixed 3 potential memory leaks in QuadTree.c 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. --- diff --git a/lib/sparse/QuadTree.c b/lib/sparse/QuadTree.c index aeb14254d..424f4dc41 100644 --- a/lib/sparse/QuadTree.c +++ b/lib/sparse/QuadTree.c @@ -356,7 +356,12 @@ QuadTree QuadTree_new_from_point_list(int dim, int n, int max_level, real *coord 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];