From patchwork Tue Dec 2 07:39:14 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fam Zheng X-Patchwork-Id: 416728 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id D405114009B for ; Tue, 2 Dec 2014 18:39:59 +1100 (AEDT) Received: from localhost ([::1]:35462 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Xvi3x-0007fX-PJ for incoming@patchwork.ozlabs.org; Tue, 02 Dec 2014 02:39:57 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:49279) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Xvi3K-0006iN-JN for qemu-devel@nongnu.org; Tue, 02 Dec 2014 02:39:26 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Xvi3E-0003yj-DB for qemu-devel@nongnu.org; Tue, 02 Dec 2014 02:39:18 -0500 Received: from mx1.redhat.com ([209.132.183.28]:37458) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Xvi3E-0003yX-5p for qemu-devel@nongnu.org; Tue, 02 Dec 2014 02:39:12 -0500 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id sB27dB7C024640 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL) for ; Tue, 2 Dec 2014 02:39:11 -0500 Received: from fam-t430.nay.redhat.com (dhcp-14-161.nay.redhat.com [10.66.14.161]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id sB27csYR011783; Tue, 2 Dec 2014 02:39:08 -0500 From: Fam Zheng To: qemu-devel@nongnu.org Date: Tue, 2 Dec 2014 15:39:14 +0800 Message-Id: <1417505957-2666-4-git-send-email-famz@redhat.com> In-Reply-To: <1417505957-2666-1-git-send-email-famz@redhat.com> References: <1417505957-2666-1-git-send-email-famz@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.22 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: Kevin Wolf , Markus Armbruster , Stefan Hajnoczi Subject: [Qemu-devel] [PATCH for-2.3 3/6] vmdk: Clean up descriptor file reading X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org 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..0c5769c 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 = MIN(size, 1 << 20); /* avoid unbounded allocation */ - 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); if (ret < 0) { error_setg_errno(errp, -ret, "Could not read from file"); g_free(buf); return NULL; } + buf[ret - 1] = 0; return buf; }