]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
Fix mem leak in virQEMUCapsProbeQMPMachineTypes on OOM
authorDaniel P. Berrange <berrange@redhat.com>
Wed, 6 Nov 2013 07:30:16 +0000 (15:30 +0800)
committerDaniel P. Berrange <berrange@redhat.com>
Tue, 12 Nov 2013 10:47:30 +0000 (10:47 +0000)
The virQEMUCapsProbeQMPMachineTypes method iterates over machine
types copying them into the qemuCapsPtr object. It only updates
the qemuCaps->nmachinetypes value at the end though. So if OOM
occurs in the middle, the destructor of qemuCapsPtr will not
free the partially initialized machine types.

Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
src/qemu/qemu_capabilities.c

index 71a913b5075f1d233ce44d46ab056ec0358dde2c..2712a4defdaba6ce2309d5a2565e59827bbbb514 100644 (file)
@@ -2137,14 +2137,17 @@ virQEMUCapsProbeQMPMachineTypes(virQEMUCapsPtr qemuCaps,
         goto cleanup;
 
     for (i = 0; i < nmachines; i++) {
-        if (VIR_STRDUP(qemuCaps->machineAliases[i], machines[i]->alias) < 0 ||
-            VIR_STRDUP(qemuCaps->machineTypes[i], machines[i]->name) < 0)
+        qemuCaps->nmachineTypes++;
+        if (VIR_STRDUP(qemuCaps->machineAliases[qemuCaps->nmachineTypes -1],
+                       machines[i]->alias) < 0 ||
+            VIR_STRDUP(qemuCaps->machineTypes[qemuCaps->nmachineTypes - 1],
+                       machines[i]->name) < 0)
             goto cleanup;
         if (machines[i]->isDefault)
             defIdx = i;
-        qemuCaps->machineMaxCpus[i] = machines[i]->maxCpus;
+        qemuCaps->machineMaxCpus[qemuCaps->nmachineTypes - 1] =
+            machines[i]->maxCpus;
     }
-    qemuCaps->nmachineTypes = nmachines;
 
     if (defIdx)
         virQEMUCapsSetDefaultMachine(qemuCaps, defIdx);