[libvirt] [PATCH 07/10] qemu: avoid memory leak on vcpupin

Eric Blake eblake at redhat.com
Thu Jun 2 23:07:59 UTC 2011


Detected by Coverity.  This leaked a cpumap on every iteration
of the loop.  Leak introduced in commit 1cc4d02 (v0.9.0).

* src/qemu/qemu_process.c (qemuProcessSetVcpuAffinites): Plug
leak, and hoist allocation outside loop.
---
 src/qemu/qemu_process.c |   21 +++++++++++++--------
 1 files changed, 13 insertions(+), 8 deletions(-)

diff --git a/src/qemu/qemu_process.c b/src/qemu/qemu_process.c
index 116253e..f175d50 100644
--- a/src/qemu/qemu_process.c
+++ b/src/qemu/qemu_process.c
@@ -1195,6 +1195,8 @@ qemuProcessSetVcpuAffinites(virConnectPtr conn,
     pid_t vcpupid;
     unsigned char *cpumask;
     int vcpu, cpumaplen, hostcpus, maxcpu;
+    unsigned char *cpumap = NULL;
+    int ret = -1;

     if (virNodeGetInfo(conn, &nodeinfo) != 0) {
         return  -1;
@@ -1216,18 +1218,18 @@ qemuProcessSetVcpuAffinites(virConnectPtr conn,
     if (maxcpu > hostcpus)
         maxcpu = hostcpus;

+    if (VIR_ALLOC_N(cpumap, cpumaplen) < 0) {
+        virReportOOMError();
+        return -1;
+    }
+
     for (vcpu = 0; vcpu < def->cputune.nvcpupin; vcpu++) {
         if (vcpu != def->cputune.vcpupin[vcpu]->vcpuid)
             continue;

         int i;
-        unsigned char *cpumap = NULL;
-
-        if (VIR_ALLOC_N(cpumap, cpumaplen) < 0) {
-            virReportOOMError();
-            return -1;
-        }

+        memset(cpumap, 0, cpumaplen);
         cpumask = (unsigned char *)def->cputune.vcpupin[vcpu]->cpumask;
         vcpupid = priv->vcpupids[vcpu];

@@ -1249,11 +1251,14 @@ qemuProcessSetVcpuAffinites(virConnectPtr conn,
                                       cpumap,
                                       cpumaplen,
                                       maxcpu) < 0) {
-            return -1;
+            goto cleanup;
         }
     }

-    return 0;
+    ret = 0;
+cleanup:
+    VIR_FREE(cpumap);
+    return ret;
 }

 static int
-- 
1.7.4.4




More information about the libvir-list mailing list