From mboxrd@z Thu Jan 1 00:00:00 1970 From: Andrew Cooper Subject: Re: [PATCH 08/13 v3] libxl: don't leak ptr in libxl_list_vm error case Date: Mon, 2 Dec 2013 10:35:35 +0000 Message-ID: <529C6277.1060609@citrix.com> References: <1385944665-29525-1-git-send-email-mattd@bugfuzz.com> <1385953126-21506-1-git-send-email-mattd@bugfuzz.com> Mime-Version: 1.0 Content-Type: text/plain; charset="windows-1252" Content-Transfer-Encoding: quoted-printable Return-path: In-Reply-To: <1385953126-21506-1-git-send-email-mattd@bugfuzz.com> List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: Matthew Daley Cc: Stefano Stabellini , Ian Jackson , Ian Campbell , xen-devel@lists.xen.org List-Id: xen-devel@lists.xenproject.org On 02/12/13 02:58, Matthew Daley wrote: > 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 > --- > v3: Oops, min() isn't defined or used anywhere in libxl. Just use a terna= ry > expression instead. > > v2: Always allocate at least one libxl_vminfo struct for the reason given= in > the commit description. > > tools/libxl/libxl.c | 14 +++++++------- > 1 file changed, 7 insertions(+), 7 deletions(-) > > diff --git a/tools/libxl/libxl.c b/tools/libxl/libxl.c > index b112294..1a2462c 100644 > --- a/tools/libxl/libxl.c > +++ b/tools/libxl/libxl.c > @@ -674,17 +674,17 @@ libxl_vminfo * libxl_list_vm(libxl_ctx *ctx, int *n= b_vm_out) > libxl_vminfo *ptr; > int idx, i, ret; > xc_domaininfo_t info[1024]; > - int size =3D 1024; > = > - ptr =3D calloc(size, sizeof(libxl_vminfo)); > - if (!ptr) > + ret =3D xc_domain_getinfolist(ctx->xch, 1, ARRAY_SIZE(info), info); > + if (ret < 0) { > + LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "getting domain info lis= t"); > return NULL; > + } > = > - ret =3D xc_domain_getinfolist(ctx->xch, 1, 1024, info); > - if (ret<0) { > - LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "geting domain info list= "); > + ptr =3D calloc(ret ? ret : 1, sizeof(libxl_vminfo)); Now I reconsider this, a comment in the code wouldn=92t go amis, given the obscurity. Also, given -std=3Dgnu99, you can use the gcc-ism of "ret ?: 1" > + if (!ptr) > return NULL; > - } > + > for (idx =3D i =3D 0; i < ret; i++) { > if (libxl_is_stubdom(ctx, info[i].domain, NULL)) > continue;