Skip to contentSkip to conversation

plug classic realloc() memory leak.

PATCH New Jimi Xenidis 1 participant 1 message Jun 13, 2006

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 plug classic realloc() memory leak.

This patch fixes the classic bug/memory leak where realloc(3) on failure returns a NULL but the original original allocation is still valid and needs to be freed.

NOTE: diff is wierd bcause the file contains hard-tabs and thats a no-no.

--

tools/xenstat/libxenstat/src/xenstat.c +5 -3
diff -r 7b25f1309eb1 tools/xenstat/libxenstat/src/xenstat.c
@@ -224,18 +224,20 @@ xenstat_node *xenstat_get_node(xenstat_h
 	num_domains = 0;
 	do {
 		xenstat_domain *domain;
+        xenstat_domain *tmp;
 
 		new_domains = xc_domain_getinfolist(handle->xc_handle,
 			num_domains, DOMAIN_CHUNK_SIZE, domaininfo);
 
-		node->domains = realloc(node->domains,
+        tmp = realloc(node->domains,
 					(num_domains + new_domains)
 					* sizeof(xenstat_domain));
-		if (node->domains == NULL) {
+        if (tmp == NULL) {
+            free(node->domains);
 			free(node);
 			return NULL;
 		}
-
+        node->domains = tmp;
 		domain = node->domains + num_domains;
 
 		for (i = 0; i < new_domains; i++) {