From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:45162) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XJTDc-0006RR-GP for qemu-devel@nongnu.org; Mon, 18 Aug 2014 16:07:58 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1XJTDW-0005Vf-CS for qemu-devel@nongnu.org; Mon, 18 Aug 2014 16:07:52 -0400 Received: from mx1.redhat.com ([209.132.183.28]:29502) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XJTDW-0005VV-51 for qemu-devel@nongnu.org; Mon, 18 Aug 2014 16:07:46 -0400 Received: from int-mx13.intmail.prod.int.phx2.redhat.com (int-mx13.intmail.prod.int.phx2.redhat.com [10.5.11.26]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id s7IK7jPw009470 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Mon, 18 Aug 2014 16:07:45 -0400 From: Max Reitz Date: Mon, 18 Aug 2014 22:07:32 +0200 Message-Id: <1408392454-22044-3-git-send-email-mreitz@redhat.com> In-Reply-To: <1408392454-22044-1-git-send-email-mreitz@redhat.com> References: <1408392454-22044-1-git-send-email-mreitz@redhat.com> Subject: [Qemu-devel] [PATCH v2 2/4] qcow2: Use g_try_new0() for cache array List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: Kevin Wolf , Stefan Hajnoczi , Max Reitz With a variable cache size, the number given to qcow2_cache_create() may be huge. Therefore, use g_try_new0(). While at it, use g_new0() instead of g_malloc0() for allocating the Qcow2Cache object. Signed-off-by: Max Reitz --- block/qcow2-cache.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/block/qcow2-cache.c b/block/qcow2-cache.c index fe0615a..904f6b1 100644 --- a/block/qcow2-cache.c +++ b/block/qcow2-cache.c @@ -48,9 +48,12 @@ Qcow2Cache *qcow2_cache_create(BlockDriverState *bs, int num_tables) Qcow2Cache *c; int i; - c = g_malloc0(sizeof(*c)); + c = g_new0(Qcow2Cache, 1); c->size = num_tables; - c->entries = g_new0(Qcow2CachedTable, num_tables); + c->entries = g_try_new0(Qcow2CachedTable, num_tables); + if (!c->entries) { + goto fail; + } for (i = 0; i < c->size; i++) { c->entries[i].table = qemu_try_blockalign(bs->file, s->cluster_size); @@ -62,8 +65,10 @@ Qcow2Cache *qcow2_cache_create(BlockDriverState *bs, int num_tables) return c; fail: - for (i = 0; i < c->size; i++) { - qemu_vfree(c->entries[i].table); + if (c->entries) { + for (i = 0; i < c->size; i++) { + qemu_vfree(c->entries[i].table); + } } g_free(c->entries); g_free(c); -- 2.0.4