Skip to contentSkip to conversation

xen-block: stop leaking memory in xen_block_drive_create()

PATCH Peter Maydell 3 participants 5 messages Feb 19, 2019 – Feb 25, 2019 1 message not in the archive

lore
Share & tools
lore
#1 lore raw
In reply to a message not in the archive

Hi Paul -- you typoed the qemu-devel list email address; cc'd the right one.

thanks
-- PMM

Quoted 77 lines · Paul Durrant

The locally allocated QDict-s need to be freed. ('file_layer' will be freed implicitly since it is added as an object to 'driver_layer').

Spotted by Coverity: CID 1398649

While in the neighbourhood free 'driver' and 'filename' as soon as they are added to the QDicts. Freeing after the 'done' label doesn't make that much sense as, if the error path jumps to that label, the values would be NULL anyway.

This patch also makes that more obvious by taking the error path if 'params' is NULL and then asserting that both driver and filename are non-NULL in the normal path.

---
Cc: Stefano Stabellini <[email protected]>
Cc: Anthony Perard <[email protected]>
Cc: Kevin Wolf <[email protected]>
Cc: Max Reitz <[email protected]>

---
 hw/block/xen-block.c | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)
hw/block/xen-block.c +9 -8
index 37a456c207..70fc2455e8 100644
@@ -743,12 +743,12 @@ static XenBlockDrive *xen_block_drive_create(const char *id,
         }
 
         g_strfreev(v);
-    }
-
-    if (!filename) {
-        error_setg(errp, "no filename");
+    } else {
+        error_setg(errp, "no params");
         goto done;
     }
+
+    assert(filename);
     assert(driver);
 
     drive = g_new0(XenBlockDrive, 1);
@@ -758,6 +758,7 @@ static XenBlockDrive *xen_block_drive_create(const char *id,
 
     qdict_put_str(file_layer, "driver", "file");
     qdict_put_str(file_layer, "filename", filename);
+    g_free(filename);
 
     if (mode && *mode != 'w') {
         qdict_put_bool(file_layer, "read-only", true);
@@ -793,16 +794,17 @@ static XenBlockDrive *xen_block_drive_create(const char *id,
     driver_layer = qdict_new();
 
     qdict_put_str(driver_layer, "driver", driver);
+    g_free(driver);
+
     qdict_put_obj(driver_layer, "file", QOBJECT(file_layer));
 
     g_assert(!drive->node_name);
     drive->node_name = xen_block_blockdev_add(drive->id, driver_layer,
                                               &local_err);
 
-done:
-    g_free(driver);
-    g_free(filename);
+    qobject_unref(driver_layer);
 
+done:
     if (local_err) {
         error_propagate(errp, local_err);
         xen_block_drive_destroy(drive, NULL);
--
Quoted 1 line

2.20.1.2.gb21ebb6

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#2 lore raw
In reply to a message not in the archive

Hi Paul -- you typoed the qemu-devel list email address; cc'd the right one.

thanks
-- PMM

Quoted 78 lines · Paul Durrant

The locally allocated QDict-s need to be freed. ('file_layer' will be freed implicitly since it is added as an object to 'driver_layer').

Spotted by Coverity: CID 1398649

While in the neighbourhood free 'driver' and 'filename' as soon as they are added to the QDicts. Freeing after the 'done' label doesn't make that much sense as, if the error path jumps to that label, the values would be NULL anyway.

This patch also makes that more obvious by taking the error path if 'params' is NULL and then asserting that both driver and filename are non-NULL in the normal path.

---
Cc: Stefano Stabellini <[email protected]>
Cc: Anthony Perard <[email protected]>
Cc: Kevin Wolf <[email protected]>
Cc: Max Reitz <[email protected]>

---
 hw/block/xen-block.c | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)
hw/block/xen-block.c +9 -8
index 37a456c207..70fc2455e8 100644
@@ -743,12 +743,12 @@ static XenBlockDrive *xen_block_drive_create(const char *id,
         }
 
         g_strfreev(v);
-    }
-
-    if (!filename) {
-        error_setg(errp, "no filename");
+    } else {
+        error_setg(errp, "no params");
         goto done;
     }
+
+    assert(filename);
     assert(driver);
 
     drive = g_new0(XenBlockDrive, 1);
@@ -758,6 +758,7 @@ static XenBlockDrive *xen_block_drive_create(const char *id,
 
     qdict_put_str(file_layer, "driver", "file");
     qdict_put_str(file_layer, "filename", filename);
+    g_free(filename);
 
     if (mode && *mode != 'w') {
         qdict_put_bool(file_layer, "read-only", true);
@@ -793,16 +794,17 @@ static XenBlockDrive *xen_block_drive_create(const char *id,
     driver_layer = qdict_new();
 
     qdict_put_str(driver_layer, "driver", driver);
+    g_free(driver);
+
     qdict_put_obj(driver_layer, "file", QOBJECT(file_layer));
 
     g_assert(!drive->node_name);
     drive->node_name = xen_block_blockdev_add(drive->id, driver_layer,
                                               &local_err);
 
-done:
-    g_free(driver);
-    g_free(filename);
+    qobject_unref(driver_layer);
 
+done:
     if (local_err) {
         error_propagate(errp, local_err);
         xen_block_drive_destroy(drive, NULL);
--

2.20.1.2.gb21ebb6

#3 lore raw
In reply to a message not in the archive

Apologies... typo-ed qemu-devel...

Quoted 91 lines

-----Original Message-----
From: Paul Durrant [mailto:[email protected]]
Sent: 19 February 2019 16:35
To: [email protected]; [email protected]; xen- [email protected]
Cc: Paul Durrant <[email protected]>; Peter Maydell <[email protected]>; Stefano Stabellini <[email protected]>; Anthony Perard <[email protected]>; Kevin Wolf <[email protected]>; Max Reitz <[email protected]>
Subject: [PATCH] xen-block: stop leaking memory in xen_block_drive_create()

The locally allocated QDict-s need to be freed. ('file_layer' will be freed implicitly since it is added as an object to 'driver_layer').

Spotted by Coverity: CID 1398649

While in the neighbourhood free 'driver' and 'filename' as soon as they are
added to the QDicts. Freeing after the 'done' label doesn't make that much sense as, if the error path jumps to that label, the values would be NULL anyway.

This patch also makes that more obvious by taking the error path if 'params' is NULL and then asserting that both driver and filename are non-NULL in the normal path.

---
Cc: Stefano Stabellini <[email protected]>
Cc: Anthony Perard <[email protected]>
Cc: Kevin Wolf <[email protected]>
Cc: Max Reitz <[email protected]>

---
 hw/block/xen-block.c | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)
hw/block/xen-block.c +4 -4
index 37a456c207..70fc2455e8 100644
@@ -743,12 +743,12 @@ static XenBlockDrive *xen_block_drive_create(const
 char *id,
         }
 
         g_strfreev(v);
-    }
-
-    if (!filename) {
-        error_setg(errp, "no filename");
+    } else {
+        error_setg(errp, "no params");
         goto done;
     }
+
+    assert(filename);
     assert(driver);
 
     drive = g_new0(XenBlockDrive, 1);
hw/block/xen-block.c +1 -0
@@ -758,6 +758,7 @@ static XenBlockDrive *xen_block_drive_create(const
 char *id,
 
     qdict_put_str(file_layer, "driver", "file");
     qdict_put_str(file_layer, "filename", filename);
+    g_free(filename);
 
     if (mode && *mode != 'w') {
         qdict_put_bool(file_layer, "read-only", true);
hw/block/xen-block.c +4 -3
@@ -793,16 +794,17 @@ static XenBlockDrive *xen_block_drive_create(const
 char *id,
     driver_layer = qdict_new();
 
     qdict_put_str(driver_layer, "driver", driver);
+    g_free(driver);
+
     qdict_put_obj(driver_layer, "file", QOBJECT(file_layer));
 
     g_assert(!drive->node_name);
     drive->node_name = xen_block_blockdev_add(drive->id, driver_layer,
                                               &local_err);
 
-done:
-    g_free(driver);
-    g_free(filename);
+    qobject_unref(driver_layer);
 
+done:
     if (local_err) {
         error_propagate(errp, local_err);
         xen_block_drive_destroy(drive, NULL);
Quoted 2 lines

--
2.20.1.2.gb21ebb6

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#4 lore raw
In reply to a message not in the archive

Apologies... typo-ed qemu-devel...

Quoted 93 lines

-----Original Message-----
From: Paul Durrant [mailto:[email protected]]
Sent: 19 February 2019 16:35
To: [email protected]; [email protected]; xen- [email protected]
Cc: Paul Durrant <[email protected]>; Peter Maydell <[email protected]>; Stefano Stabellini <[email protected]>; Anthony Perard <[email protected]>; Kevin Wolf <[email protected]>; Max Reitz <[email protected]>
Subject: [PATCH] xen-block: stop leaking memory in xen_block_drive_create()

The locally allocated QDict-s need to be freed. ('file_layer' will be freed implicitly since it is added as an object to 'driver_layer').

Spotted by Coverity: CID 1398649

While in the neighbourhood free 'driver' and 'filename' as soon as they are
added to the QDicts. Freeing after the 'done' label doesn't make that much sense as, if the error path jumps to that label, the values would be NULL anyway.

This patch also makes that more obvious by taking the error path if 'params' is NULL and then asserting that both driver and filename are non-NULL in the normal path.

---
Cc: Stefano Stabellini <[email protected]>
Cc: Anthony Perard <[email protected]>
Cc: Kevin Wolf <[email protected]>
Cc: Max Reitz <[email protected]>

---
 hw/block/xen-block.c | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)
hw/block/xen-block.c +4 -4
index 37a456c207..70fc2455e8 100644
@@ -743,12 +743,12 @@ static XenBlockDrive *xen_block_drive_create(const
 char *id,
         }
 
         g_strfreev(v);
-    }
-
-    if (!filename) {
-        error_setg(errp, "no filename");
+    } else {
+        error_setg(errp, "no params");
         goto done;
     }
+
+    assert(filename);
     assert(driver);
 
     drive = g_new0(XenBlockDrive, 1);
hw/block/xen-block.c +1 -0
@@ -758,6 +758,7 @@ static XenBlockDrive *xen_block_drive_create(const
 char *id,
 
     qdict_put_str(file_layer, "driver", "file");
     qdict_put_str(file_layer, "filename", filename);
+    g_free(filename);
 
     if (mode && *mode != 'w') {
         qdict_put_bool(file_layer, "read-only", true);
hw/block/xen-block.c +4 -3
@@ -793,16 +794,17 @@ static XenBlockDrive *xen_block_drive_create(const
 char *id,
     driver_layer = qdict_new();
 
     qdict_put_str(driver_layer, "driver", driver);
+    g_free(driver);
+
     qdict_put_obj(driver_layer, "file", QOBJECT(file_layer));
 
     g_assert(!drive->node_name);
     drive->node_name = xen_block_blockdev_add(drive->id, driver_layer,
                                               &local_err);
 
-done:
-    g_free(driver);
-    g_free(filename);
+    qobject_unref(driver_layer);
 
+done:
     if (local_err) {
         error_propagate(errp, local_err);
         xen_block_drive_destroy(drive, NULL);

--
2.20.1.2.gb21ebb6

#5 lore raw
Quoted 11 lines · Paul Durrant
Quoted 11 lines

The locally allocated QDict-s need to be freed. ('file_layer' will be freed implicitly since it is added as an object to 'driver_layer').

Spotted by Coverity: CID 1398649

While in the neighbourhood free 'driver' and 'filename' as soon as they are
added to the QDicts. Freeing after the 'done' label doesn't make that much sense as, if the error path jumps to that label, the values would be NULL anyway.

Quoted 6 lines
Quoted 6 lines

This patch also makes that more obvious by taking the error path if 'params' is NULL and then asserting that both driver and filename are non-NULL in the normal path.

Thanks,

Signature · 6 lines
Anthony PERARD

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel