From 2834d57175f941021ee4dad1beb9f0beec1c38a4 Mon Sep 17 00:00:00 2001 From: Eric Blake Date: Thu, 2 Jun 2011 16:47:53 -0600 Subject: [PATCH] qemu: avoid memory leak on vcpupin 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 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/qemu/qemu_process.c b/src/qemu/qemu_process.c index 116253e762..f175d50823 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 -- GitLab