qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH] qcow2: Use g_try_realloc() in qcow2_expand_zero_clusters()
@ 2018-02-09 14:42 Alberto Garcia
  2018-02-09 14:55 ` Kevin Wolf
  0 siblings, 1 reply; 2+ messages in thread
From: Alberto Garcia @ 2018-02-09 14:42 UTC (permalink / raw)
  To: qemu-devel; +Cc: Alberto Garcia, qemu-block, Max Reitz, Kevin Wolf, Eric Blake

g_realloc() aborts the program if it fails to allocate the required
amount of memory. We want to detect that scenario and return an error
instead, so let's use g_try_realloc().

Signed-off-by: Alberto Garcia <berto@igalia.com>
---
 block/qcow2-cluster.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/block/qcow2-cluster.c b/block/qcow2-cluster.c
index a3fec27bf9..add6d86052 100644
--- a/block/qcow2-cluster.c
+++ b/block/qcow2-cluster.c
@@ -2071,7 +2071,15 @@ int qcow2_expand_zero_clusters(BlockDriverState *bs,
         int l1_sectors = DIV_ROUND_UP(s->snapshots[i].l1_size *
                                       sizeof(uint64_t), BDRV_SECTOR_SIZE);
 
-        l1_table = g_realloc(l1_table, l1_sectors * BDRV_SECTOR_SIZE);
+        uint64_t *new_l1_table =
+            g_try_realloc(l1_table, l1_sectors * BDRV_SECTOR_SIZE);
+
+        if (!new_l1_table) {
+            ret = -ENOMEM;
+            goto fail;
+        }
+
+        l1_table = new_l1_table;
 
         ret = bdrv_read(bs->file,
                         s->snapshots[i].l1_table_offset / BDRV_SECTOR_SIZE,
-- 
2.11.0

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [Qemu-devel] [PATCH] qcow2: Use g_try_realloc() in qcow2_expand_zero_clusters()
  2018-02-09 14:42 [Qemu-devel] [PATCH] qcow2: Use g_try_realloc() in qcow2_expand_zero_clusters() Alberto Garcia
@ 2018-02-09 14:55 ` Kevin Wolf
  0 siblings, 0 replies; 2+ messages in thread
From: Kevin Wolf @ 2018-02-09 14:55 UTC (permalink / raw)
  To: Alberto Garcia; +Cc: qemu-devel, qemu-block, Max Reitz, Eric Blake

Am 09.02.2018 um 15:42 hat Alberto Garcia geschrieben:
> g_realloc() aborts the program if it fails to allocate the required
> amount of memory. We want to detect that scenario and return an error
> instead, so let's use g_try_realloc().
> 
> Signed-off-by: Alberto Garcia <berto@igalia.com>

Thanks, applied to the block branch.

Kevin

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2018-02-09 14:56 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-02-09 14:42 [Qemu-devel] [PATCH] qcow2: Use g_try_realloc() in qcow2_expand_zero_clusters() Alberto Garcia
2018-02-09 14:55 ` Kevin Wolf

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).