From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:47668) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XKqCP-000108-5c for qemu-devel@nongnu.org; Fri, 22 Aug 2014 10:52:23 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1XKqCI-0005cp-Pp for qemu-devel@nongnu.org; Fri, 22 Aug 2014 10:52:17 -0400 Received: from mx1.redhat.com ([209.132.183.28]:27642) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XKqCI-0005ck-Il for qemu-devel@nongnu.org; Fri, 22 Aug 2014 10:52:10 -0400 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 s7MEq97I003036 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Fri, 22 Aug 2014 10:52:10 -0400 From: Kevin Wolf Date: Fri, 22 Aug 2014 16:51:32 +0200 Message-Id: <1408719113-5316-9-git-send-email-kwolf@redhat.com> In-Reply-To: <1408719113-5316-1-git-send-email-kwolf@redhat.com> References: <1408719113-5316-1-git-send-email-kwolf@redhat.com> Subject: [Qemu-devel] [PULL 08/29] 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: kwolf@redhat.com From: 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 Signed-off-by: Kevin Wolf --- 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); -- 1.8.3.1