From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:44048) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Xw66v-0003M7-Tb for qemu-devel@nongnu.org; Wed, 03 Dec 2014 04:20:44 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Xw66p-0007T5-9J for qemu-devel@nongnu.org; Wed, 03 Dec 2014 04:20:37 -0500 Received: from mx1.redhat.com ([209.132.183.28]:47330) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Xw66o-0007Sy-FQ for qemu-devel@nongnu.org; Wed, 03 Dec 2014 04:20:30 -0500 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id sB39KTAJ029844 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL) for ; Wed, 3 Dec 2014 04:20:29 -0500 Date: Wed, 3 Dec 2014 17:20:27 +0800 From: Fam Zheng Message-ID: <20141203092027.GA14611@ad.nay.redhat.com> References: <1417572314-5504-1-git-send-email-famz@redhat.com> <1417572314-5504-4-git-send-email-famz@redhat.com> <87fvcxqh2c.fsf@blackfin.pond.sub.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <87fvcxqh2c.fsf@blackfin.pond.sub.org> Subject: Re: [Qemu-devel] [PATCH for-2.3 v2 3/6] vmdk: Clean up descriptor file reading List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Markus Armbruster Cc: Kevin Wolf , qemu-devel@nongnu.org, Stefan Hajnoczi On Wed, 12/03 09:21, Markus Armbruster wrote: > Fam Zheng writes: > > > Zeroing a buffer that will be filled right after is not necessary, and > > allocating a power of two + 1 is naughty. > > > > Suggested-by: Markus Armbruster > > Signed-off-by: Fam Zheng > > --- > > block/vmdk.c | 5 +++-- > > 1 file changed, 3 insertions(+), 2 deletions(-) > > > > diff --git a/block/vmdk.c b/block/vmdk.c > > index 28d22db..e863a09 100644 > > --- a/block/vmdk.c > > +++ b/block/vmdk.c > > @@ -558,14 +558,15 @@ static char *vmdk_read_desc(BlockDriverState *file, uint64_t desc_offset, > > } > > > size = bdrv_getlength(file); > if (size < 0) { > error_setg_errno(errp, -size, "Could not access file"); > return NULL; > } > > > size = MIN(size, 1 << 20); /* avoid unbounded allocation */ > > Consider the case where size <= 1 << 20, i.e. this line is a no-op. > > > - buf = g_malloc0(size + 1); > > + buf = g_malloc(size); > > > > - ret = bdrv_pread(file, desc_offset, buf, size); > > + ret = bdrv_pread(file, desc_offset, buf, size - 1); > > Then this reads everything except the last byte (thanks to Don for > spotting it). Yes, I was wrong. > > > if (ret < 0) { > > error_setg_errno(errp, -ret, "Could not read from file"); > > g_free(buf); > > return NULL; > > } > > + buf[ret] = 0; > > > > return buf; > > } > > I figure Don suggested this instead: Yes. Thanks. Fam > > diff --git a/block/vmdk.c b/block/vmdk.c > index 2cbfd3e..b7feb15 100644 > --- a/block/vmdk.c > +++ b/block/vmdk.c > @@ -556,8 +556,8 @@ static char *vmdk_read_desc(BlockDriverState *file, uint64_t desc_offset, > return NULL; > } > > - size = MIN(size, 1 << 20); /* avoid unbounded allocation */ > - buf = g_malloc0(size + 1); > + size = MIN(size, (1 << 20) - 1); /* avoid unbounded allocation */ > + buf = g_malloc(size + 1); > > ret = bdrv_pread(file, desc_offset, buf, size); > if (ret < 0) { > @@ -565,6 +565,7 @@ static char *vmdk_read_desc(BlockDriverState *file, uint64_t desc_offset, > g_free(buf); > return NULL; > } > + buf[ret] = 0; > > return buf; > }