From mboxrd@z Thu Jan 1 00:00:00 1970 From: Dario Faggioli Subject: [PATCH v5 03/17] libxl: fix memory leak in libxl_list_vcpu Date: Mon, 02 Dec 2013 19:27:41 +0100 Message-ID: <20131202182741.29026.99639.stgit@Solace> References: <20131202180129.29026.81543.stgit@Solace> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Return-path: In-Reply-To: <20131202180129.29026.81543.stgit@Solace> List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: xen-devel@lists.xen.org Cc: Marcus Granado , Keir Fraser , Ian Campbell , Li Yechen , George Dunlap , Andrew Cooper , Juergen Gross , Ian Jackson , Jan Beulich , Justin Weaver , Matt Wilson , Elena Ufimtseva List-Id: xen-devel@lists.xenproject.org more specifically, of the cpumap inside libxl_vcpuinfo, in case of failure after it has been allocated. While at it, use the correct libxl memory allocation wrapper for calloc() in there and turn the function into using the new LOGE() logging style. Signed-off-by: Dario Faggioli --- Changes from v4: * new patch, wasn't there in v4, but leak was identified during review. --- tools/libxl/libxl.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/tools/libxl/libxl.c b/tools/libxl/libxl.c index 0cb7ca9..028106b 100644 --- a/tools/libxl/libxl.c +++ b/tools/libxl/libxl.c @@ -4527,31 +4527,31 @@ const libxl_version_info* libxl_get_version_info(libxl_ctx *ctx) libxl_vcpuinfo *libxl_list_vcpu(libxl_ctx *ctx, uint32_t domid, int *nb_vcpu, int *nr_cpus_out) { + GC_INIT(ctx); libxl_vcpuinfo *ptr, *ret; xc_domaininfo_t domaininfo; xc_vcpuinfo_t vcpuinfo; if (xc_domain_getinfolist(ctx->xch, domid, 1, &domaininfo) != 1) { - LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "getting infolist"); + LOGE(ERROR, "getting infolist"); return NULL; } *nr_cpus_out = libxl_get_max_cpus(ctx); - ret = ptr = calloc(domaininfo.max_vcpu_id + 1, sizeof (libxl_vcpuinfo)); - if (!ptr) { - return NULL; - } + ret = ptr = libxl__calloc(NOGC, domaininfo.max_vcpu_id + 1, + sizeof(libxl_vcpuinfo)); for (*nb_vcpu = 0; *nb_vcpu <= domaininfo.max_vcpu_id; ++*nb_vcpu, ++ptr) { + libxl_bitmap_init(&ptr->cpumap); if (libxl_cpu_bitmap_alloc(ctx, &ptr->cpumap, 0)) { - LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "allocating cpumap"); + LOGE(ERROR, "allocating cpumap"); goto err; } if (xc_vcpu_getinfo(ctx->xch, domid, *nb_vcpu, &vcpuinfo) == -1) { - LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "getting vcpu info"); + LOGE(ERROR, "getting vcpu info"); goto err; } if (xc_vcpu_getaffinity(ctx->xch, domid, *nb_vcpu, ptr->cpumap.map) == -1) { - LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "getting vcpu affinity"); + LOGE(ERROR, "getting vcpu affinity"); goto err; } ptr->vcpuid = *nb_vcpu; @@ -4561,10 +4561,13 @@ libxl_vcpuinfo *libxl_list_vcpu(libxl_ctx *ctx, uint32_t domid, ptr->running = !!vcpuinfo.running; ptr->vcpu_time = vcpuinfo.cpu_time; } + GC_FREE; return ret; err: + libxl_bitmap_dispose(&ptr->cpumap); free(ret); + GC_FREE; return NULL; }