From: Jimi Xenidis Subject: [patch] plug classic realloc() memory leak. Date: Tue, 13 Jun 2006 17:42:28 -0400 Message-ID: Return-path: List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xensource.com Errors-To: xen-devel-bounces@lists.xensource.com To: xen-devel@lists.xensource.com List-Id: xen-devel@lists.xenproject.org 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 -- diff -r 7b25f1309eb1 tools/xenstat/libxenstat/src/xenstat.c --- a/tools/xenstat/libxenstat/src/xenstat.c Tue Jun 13 15:14:57 2006 -0400 +++ b/tools/xenstat/libxenstat/src/xenstat.c Tue Jun 13 17:18:36 2006 -0400 @@ -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++) {