plug classic realloc() memory leak.
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.
Signed-off-by Jimi Xenidis <[email protected]>
--
tools/xenstat/libxenstat/src/xenstat.c +5 -3
@@ -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++) {