[libvirt] [PATCH] Fix memory leak in qemuBuildDriveStr()

Nehal J Wani nehaljw.kkd1 at gmail.com
Wed Dec 4 20:23:41 UTC 2013


This patch fixes memory leaks reported by valgrind on running qemuxml2argvtest

Most of them are of the form:

==24777== 15 bytes in 1 blocks are definitely lost in loss record 39 of 129
==24777==    at 0x4A0887C: malloc (vg_replace_malloc.c:270)
==24777==    by 0x341F485E21: strdup (strdup.c:42)
==24777==    by 0x4CADE5F: virStrdup (virstring.c:554)
==24777==    by 0x4362B6: qemuBuildDriveStr (qemu_command.c:3848)
==24777==    by 0x43EF73: qemuBuildCommandLine (qemu_command.c:8500)
==24777==    by 0x426670: testCompareXMLToArgvHelper (qemuxml2argvtest.c:350)
==24777==    by 0x427C01: virtTestRun (testutils.c:138)
==24777==    by 0x41DDB5: mymain (qemuxml2argvtest.c:658)
==24777==    by 0x4282A2: virtTestMain (testutils.c:593)
==24777==    by 0x341F421A04: (below main) (libc-start.c:225)
==24777==

---
 src/qemu/qemu_command.c | 74 +++++++++++++++++++++++++------------------------
 1 file changed, 38 insertions(+), 36 deletions(-)

diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c
index 768fdc4..6a30a29 100644
--- a/src/qemu/qemu_command.c
+++ b/src/qemu/qemu_command.c
@@ -4016,52 +4016,54 @@ qemuBuildDriveStr(virConnectPtr conn,
     if (qemuDomainDiskGetSourceString(conn, disk, &source) < 0)
         goto error;
 
-    if (source &&
-        !((disk->device == VIR_DOMAIN_DISK_DEVICE_FLOPPY ||
-           disk->device == VIR_DOMAIN_DISK_DEVICE_CDROM) &&
-          disk->tray_status == VIR_DOMAIN_DISK_TRAY_OPEN)) {
+    if (source) {
+        if(!((disk->device == VIR_DOMAIN_DISK_DEVICE_FLOPPY ||
+              disk->device == VIR_DOMAIN_DISK_DEVICE_CDROM) &&
+             disk->tray_status == VIR_DOMAIN_DISK_TRAY_OPEN)) {
 
-        virBufferAddLit(&opt, "file=");
+            virBufferAddLit(&opt, "file=");
 
-        switch (actualType) {
-        case VIR_DOMAIN_DISK_TYPE_DIR:
-            /* QEMU only supports magic FAT format for now */
-            if (disk->format > 0 && disk->format != VIR_STORAGE_FILE_FAT) {
-                virReportError(VIR_ERR_INTERNAL_ERROR,
-                               _("unsupported disk driver type for '%s'"),
-                               virStorageFileFormatTypeToString(disk->format));
-                goto error;
-            }
+            switch (actualType) {
+            case VIR_DOMAIN_DISK_TYPE_DIR:
+                /* QEMU only supports magic FAT format for now */
+                if (disk->format > 0 && disk->format != VIR_STORAGE_FILE_FAT) {
+                    virReportError(VIR_ERR_INTERNAL_ERROR,
+                                   _("unsupported disk driver type for '%s'"),
+                                   virStorageFileFormatTypeToString(disk->format));
+                    goto error;
+                }
 
-            if (!disk->readonly) {
-                virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
-                               _("cannot create virtual FAT disks in read-write mode"));
-                goto error;
-            }
+                if (!disk->readonly) {
+                    virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                                   _("cannot create virtual FAT disks in read-write mode"));
+                    goto error;
+                }
 
-            virBufferAddLit(&opt, "fat:");
+                virBufferAddLit(&opt, "fat:");
 
-            if (disk->device == VIR_DOMAIN_DISK_DEVICE_FLOPPY)
-                virBufferAddLit(&opt, "floppy:");
+                if (disk->device == VIR_DOMAIN_DISK_DEVICE_FLOPPY)
+                    virBufferAddLit(&opt, "floppy:");
 
-            break;
+                break;
 
-        case VIR_DOMAIN_DISK_TYPE_BLOCK:
-            if (disk->tray_status == VIR_DOMAIN_DISK_TRAY_OPEN) {
-                virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
-                               disk->type == VIR_DOMAIN_DISK_TYPE_VOLUME ?
-                               _("tray status 'open' is invalid for block type volume") :
-                               _("tray status 'open' is invalid for block type disk"));
-                goto error;
-            }
+            case VIR_DOMAIN_DISK_TYPE_BLOCK:
+                if (disk->tray_status == VIR_DOMAIN_DISK_TRAY_OPEN) {
+                    virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
+                                   disk->type == VIR_DOMAIN_DISK_TYPE_VOLUME ?
+                                   _("tray status 'open' is invalid for block type volume") :
+                                   _("tray status 'open' is invalid for block type disk"));
+                    goto error;
+                }
 
-            break;
+                break;
 
-        default:
-            break;
-        }
+            default:
+                break;
+            }
 
-        virBufferEscape(&opt, ',', ",", "%s,", source);
+            virBufferEscape(&opt, ',', ",", "%s,", source);
+        }
+        VIR_FREE(source);
     }
 
     if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_DEVICE))
-- 
1.8.1.4




More information about the libvir-list mailing list