From mboxrd@z Thu Jan 1 00:00:00 1970 From: Matthew Daley Subject: [PATCH 08/13 v4] libxl: don't leak ptr in libxl_list_vm error case Date: Tue, 3 Dec 2013 00:05:27 +1300 Message-ID: <1385982328-21666-1-git-send-email-mattd@bugfuzz.com> References: Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Return-path: In-Reply-To: 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: Andrew Cooper , Matthew Daley , Ian Jackson , Ian Campbell , Stefano Stabellini List-Id: xen-devel@lists.xenproject.org While at it, tidy up the function; there's no point in allocating more than the amount of domains actually returned by xc_domain_getinfolist (unless 0 domains are returned, in which case we should still allocate one libxl_vminfo struct so we can return a non-NULL result and not appear to have failed from the caller's perspective.) Coverity-ID: 1055888 Signed-off-by: Matthew Daley --- v4: Add a comment describing the calloc malarkey tools/libxl/libxl.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/tools/libxl/libxl.c b/tools/libxl/libxl.c index b112294..7308d44 100644 --- a/tools/libxl/libxl.c +++ b/tools/libxl/libxl.c @@ -674,17 +674,22 @@ libxl_vminfo * libxl_list_vm(libxl_ctx *ctx, int *nb_vm_out) libxl_vminfo *ptr; int idx, i, ret; xc_domaininfo_t info[1024]; - int size = 1024; - ptr = calloc(size, sizeof(libxl_vminfo)); - if (!ptr) + ret = xc_domain_getinfolist(ctx->xch, 1, ARRAY_SIZE(info), info); + if (ret < 0) { + LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "getting domain info list"); return NULL; + } - ret = xc_domain_getinfolist(ctx->xch, 1, 1024, info); - if (ret<0) { - LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "geting domain info list"); + /* + * Always make sure to allocate at least one element; if we don't and we + * request zero, we (might) get back a null pointer, which if returned + * to our caller will make them think we've failed + */ + ptr = calloc(ret ? ret : 1, sizeof(libxl_vminfo)); + if (!ptr) return NULL; - } + for (idx = i = 0; i < ret; i++) { if (libxl_is_stubdom(ctx, info[i].domain, NULL)) continue; -- 1.7.10.4