From 83c25e485a2cb24753ff429dbe80dcd462881798 Mon Sep 17 00:00:00 2001 From: Erwin Janssen Date: Wed, 7 Dec 2016 14:56:39 +0100 Subject: [PATCH] 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. --- lib/sparse/QuadTree.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) 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]; -- 2.40.0