From: Stefan Hajnoczi Subject: [PATCH] qcow2: Fix segfault when qcow2 preallocate fails Date: Tue, 26 Oct 2010 14:23:19 +0100 Message-ID: <1288099399-10010-1-git-send-email-stefanha@linux.vnet.ibm.com> References: <4CC6D1EA.7050706@redhat.com> Cc: , Anthony Liguori , Juan Quintela , , Kevin Wolf , Stefan Hajnoczi To: Avi Kivity Return-path: Received: from mtagate3.uk.ibm.com ([194.196.100.163]:51807 "EHLO mtagate3.uk.ibm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932464Ab0JZNX1 (ORCPT ); Tue, 26 Oct 2010 09:23:27 -0400 Received: from d06nrmr1307.portsmouth.uk.ibm.com (d06nrmr1307.portsmouth.uk.ibm.com [9.149.38.129]) by mtagate3.uk.ibm.com (8.13.1/8.13.1) with ESMTP id o9QDNN28001488 for ; Tue, 26 Oct 2010 13:23:23 GMT Received: from d06av07.portsmouth.uk.ibm.com (d06av07.portsmouth.uk.ibm.com [9.149.37.248]) by d06nrmr1307.portsmouth.uk.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id o9QDNMrR3559636 for ; Tue, 26 Oct 2010 14:23:22 +0100 Received: from d06av07.portsmouth.uk.ibm.com (loopback [127.0.0.1]) by d06av07.portsmouth.uk.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id o9QDNMm5021046 for ; Tue, 26 Oct 2010 07:23:22 -0600 In-Reply-To: <4CC6D1EA.7050706@redhat.com> Sender: kvm-owner@vger.kernel.org List-ID: When an image is created with -o preallocate, ensure that we only call preallocate() if the image was indeed opened successfully. Also use bdrv_delete() instead of bdrv_close() to avoid leaking the BlockDriverState structure. This fixes the segfault reported at https://bugzilla.redhat.com/show_bug.cgi?id=646538. Signed-off-by: Stefan Hajnoczi --- Here's a fix for the segfault. block/qcow2.c | 8 +++++--- 1 files changed, 5 insertions(+), 3 deletions(-) diff --git a/block/qcow2.c b/block/qcow2.c index ee3481b..0fceb0d 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -1059,9 +1059,11 @@ exit: BlockDriverState *bs; BlockDriver *drv = bdrv_find_format("qcow2"); bs = bdrv_new(""); - bdrv_open(bs, filename, BDRV_O_CACHE_WB | BDRV_O_RDWR, drv); - ret = preallocate(bs); - bdrv_close(bs); + ret = bdrv_open(bs, filename, BDRV_O_CACHE_WB | BDRV_O_RDWR, drv); + if (ret == 0) { + ret = preallocate(bs); + } + bdrv_delete(bs); } return ret; -- 1.7.1