Skip to contentSkip to conversation

xen-block: Avoid leaks on new error path

PATCH In review Anthony PERARD via 3 participants 3 messages Jul 4, 2023 – Jul 6, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xen-block: Avoid leaks on new error path

From: Anthony PERARD <[email protected]>

Commit 189829399070 ("xen-block: Use specific blockdev driver") introduced a new error path, without taking care of allocated resources.

So only allocate the qdicts after the error check, and free both filename and driver when we are about to return and thus taking care of both success and error path.

Coverity only spotted the leak of qdicts (*_layer variables).

Fixes Coverity CID 1508722, 1398649
Fixes 189829399070 ("xen-block: Use specific blockdev driver")
---
 hw/block/xen-block.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
hw/block/xen-block.c +6 -5
index f099914831..3906b9058b 100644
@@ -781,14 +781,15 @@ static XenBlockDrive *xen_block_drive_create(const char *id,
     drive = g_new0(XenBlockDrive, 1);
     drive->id = g_strdup(id);
 
-    file_layer = qdict_new();
-    driver_layer = qdict_new();
-
     rc = stat(filename, &st);
     if (rc) {
         error_setg_errno(errp, errno, "Could not stat file '%s'", filename);
         goto done;
     }
+
+    file_layer = qdict_new();
+    driver_layer = qdict_new();
+
     if (S_ISBLK(st.st_mode)) {
         qdict_put_str(file_layer, "driver", "host_device");
     } else {
@@ -796,7 +797,6 @@ static XenBlockDrive *xen_block_drive_create(const char *id,
     }
 
     qdict_put_str(file_layer, "filename", filename);
-    g_free(filename);
 
     if (mode && *mode != 'w') {
         qdict_put_bool(file_layer, "read-only", true);
@@ -831,7 +831,6 @@ static XenBlockDrive *xen_block_drive_create(const char *id,
     qdict_put_str(file_layer, "locking", "off");
 
     qdict_put_str(driver_layer, "driver", driver);
-    g_free(driver);
 
     qdict_put(driver_layer, "file", file_layer);
 
@@ -842,6 +841,8 @@ static XenBlockDrive *xen_block_drive_create(const char *id,
     qobject_unref(driver_layer);
 
 done:
+    g_free(filename);
+    g_free(driver);
     if (*errp) {
         xen_block_drive_destroy(drive, NULL);
         return NULL;
Signature · 1 line
Anthony PERARD
Reviewed-by on v1 1/1
#1 lore raw
Quoted 11 lines · Anthony PERARD

From: Anthony PERARD <[email protected]>

Commit 189829399070 ("xen-block: Use specific blockdev driver") introduced a new error path, without taking care of allocated resources.

So only allocate the qdicts after the error check, and free both filename and driver when we are about to return and thus taking care of both success and error path.

Quoted 6 lines

Coverity only spotted the leak of qdicts (*_layer variables).

Fixes Coverity CID 1508722, 1398649
Fixes 189829399070 ("xen-block: Use specific blockdev driver")

thanks
-- PMM

Reviewed-by on v1 1/1
#2 lore raw
In reply to Anthony PERARD via
Quoted 12 lines · Anthony PERARD

From: Anthony PERARD <[email protected]>

Commit 189829399070 ("xen-block: Use specific blockdev driver") introduced a new error path, without taking care of allocated resources.

So only allocate the qdicts after the error check, and free both filename and driver when we are about to return and thus taking care of both success and error path.

Coverity only spotted the leak of qdicts (*_layer variables).

Quoted 8 lines
Fixes Coverity CID 1508722, 1398649
Fixes 189829399070 ("xen-block: Use specific blockdev driver")
---
  hw/block/xen-block.c | 11 ++++++-----
  1 file changed, 6 insertions(+), 5 deletions(-)