From patchwork Wed May 21 16:28:16 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kevin Wolf X-Patchwork-Id: 351244 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 C0305140085 for ; Thu, 22 May 2014 02:31:22 +1000 (EST) Received: from localhost ([::1]:60844 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Wn9QG-0004RO-Ag for incoming@patchwork.ozlabs.org; Wed, 21 May 2014 12:31:20 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:55171) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Wn9Nw-0001Rp-P1 for qemu-devel@nongnu.org; Wed, 21 May 2014 12:29:02 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Wn9Np-0002K5-A0 for qemu-devel@nongnu.org; Wed, 21 May 2014 12:28:56 -0400 Received: from mx1.redhat.com ([209.132.183.28]:55799) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Wn9Np-0002Jm-2x for qemu-devel@nongnu.org; Wed, 21 May 2014 12:28:49 -0400 Received: from int-mx02.intmail.prod.int.phx2.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id s4LGSmLN001840 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Wed, 21 May 2014 12:28:48 -0400 Received: from noname.redhat.com (ovpn-116-42.ams2.redhat.com [10.36.116.42]) by int-mx02.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id s4LGSJa0017108; Wed, 21 May 2014 12:28:47 -0400 From: Kevin Wolf To: qemu-devel@nongnu.org Date: Wed, 21 May 2014 18:28:16 +0200 Message-Id: <1400689698-3096-19-git-send-email-kwolf@redhat.com> In-Reply-To: <1400689698-3096-1-git-send-email-kwolf@redhat.com> References: <1400689698-3096-1-git-send-email-kwolf@redhat.com> X-Scanned-By: MIMEDefang 2.67 on 10.5.11.12 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: kwolf@redhat.com, stefanha@redhat.com Subject: [Qemu-devel] [PATCH 18/20] vmdk: Handle failure for potentially large allocations 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 Some code in the block layer makes potentially huge allocations. Failure is not completely unexpected there, so avoid aborting qemu and handle out-of-memory situations gracefully. This patch addresses the allocations in the vmdk block driver. Signed-off-by: Kevin Wolf Reviewed-by: Stefan Hajnoczi --- block/vmdk.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/block/vmdk.c b/block/vmdk.c index 480ea37..96a127d 100644 --- a/block/vmdk.c +++ b/block/vmdk.c @@ -448,7 +448,11 @@ static int vmdk_init_tables(BlockDriverState *bs, VmdkExtent *extent, /* read the L1 table */ l1_size = extent->l1_size * sizeof(uint32_t); - extent->l1_table = g_malloc(l1_size); + extent->l1_table = g_try_malloc(l1_size); + if (l1_size && extent->l1_table == NULL) { + return -ENOMEM; + } + ret = bdrv_pread(extent->file, extent->l1_table_offset, extent->l1_table, @@ -464,7 +468,11 @@ static int vmdk_init_tables(BlockDriverState *bs, VmdkExtent *extent, } if (extent->l1_backup_table_offset) { - extent->l1_backup_table = g_malloc(l1_size); + extent->l1_backup_table = g_try_malloc(l1_size); + if (l1_size && extent->l1_backup_table == NULL) { + ret = -ENOMEM; + goto fail_l1; + } ret = bdrv_pread(extent->file, extent->l1_backup_table_offset, extent->l1_backup_table,