From: Nehal J Wani Date: Thu, 27 Mar 2014 11:38:56 +0000 (+0530) Subject: Fix Memory Leak in virQEMUCapsInitGuestFromBinary() X-Git-Url: http://xenbits.xensource.com/gitweb?a=commitdiff_plain;h=faad5582e4c6063a4d84f2069745f5b53239d6d2;p=libvirt.git Fix Memory Leak in virQEMUCapsInitGuestFromBinary() While running qemucaps2xmltest, it was found that valgrind pointed out the following memory leaks: ==29896== 0 bytes in 1 blocks are definitely lost in loss record 1 of 65 ==29896== at 0x4A0577B: calloc (vg_replace_malloc.c:593) ==29896== by 0x4C6B45E: virAllocN (viralloc.c:191) ==29896== by 0x4232A9: virQEMUCapsGetMachineTypesCaps (qemu_capabilities.c:1999) ==29896== by 0x4234E7: virQEMUCapsInitGuestFromBinary (qemu_capabilities.c:789) ==29896== by 0x41F10B: testQemuCapsXML (qemucaps2xmltest.c:118) ==29896== by 0x41FFD1: virtTestRun (testutils.c:201) ==29896== by 0x41EE7A: mymain (qemucaps2xmltest.c:203) ==29896== by 0x42074D: virtTestMain (testutils.c:789) ==29896== by 0x3E6CE1ED1C: (below main) (libc-start.c:226) ==29896== ==29896== 0 bytes in 1 blocks are definitely lost in loss record 2 of 65 ==29896== at 0x4A0577B: calloc (vg_replace_malloc.c:593) ==29896== by 0x4C6B45E: virAllocN (viralloc.c:191) ==29896== by 0x4232A9: virQEMUCapsGetMachineTypesCaps (qemu_capabilities.c:1999) ==29896== by 0x4234E7: virQEMUCapsInitGuestFromBinary (qemu_capabilities.c:789) ==29896== by 0x41F10B: testQemuCapsXML (qemucaps2xmltest.c:118) ==29896== by 0x41FFD1: virtTestRun (testutils.c:201) ==29896== by 0x41EEA3: mymain (qemucaps2xmltest.c:204) ==29896== by 0x42074D: virtTestMain (testutils.c:789) ==29896== by 0x3E6CE1ED1C: (below main) (libc-start.c:226) Signed-off-by: Eric Blake --- diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c index 76735929b7..e1d04ff20b 100644 --- a/src/qemu/qemu_capabilities.c +++ b/src/qemu/qemu_capabilities.c @@ -1994,12 +1994,13 @@ int virQEMUCapsGetMachineTypesCaps(virQEMUCapsPtr qemuCaps, { size_t i; - *nmachines = 0; *machines = NULL; - if (VIR_ALLOC_N(*machines, qemuCaps->nmachineTypes) < 0) - goto error; *nmachines = qemuCaps->nmachineTypes; + if (*nmachines && + VIR_ALLOC_N(*machines, qemuCaps->nmachineTypes) < 0) + goto error; + for (i = 0; i < qemuCaps->nmachineTypes; i++) { virCapsGuestMachinePtr mach; if (VIR_ALLOC(mach) < 0)