[Qemu-devel] [PATCH] qcow2: Use g_try_realloc() in qcow2_expand_zero_clusters()

Alberto Garcia posted 1 patch 6 years, 2 months ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20180209144222.8937-1-berto@igalia.com
Test checkpatch passed
Test docker-build@min-glib passed
Test docker-mingw@fedora passed
Test docker-quick@centos6 passed
Test ppcbe passed
Test ppcle passed
Test s390x passed
block/qcow2-cluster.c | 10 +++++++++-
1 file changed, 9 insertions(+), 1 deletion(-)
[Qemu-devel] [PATCH] qcow2: Use g_try_realloc() in qcow2_expand_zero_clusters()
Posted by Alberto Garcia 6 years, 2 months ago
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


Re: [Qemu-devel] [PATCH] qcow2: Use g_try_realloc() in qcow2_expand_zero_clusters()
Posted by Kevin Wolf 6 years, 2 months ago
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