[libvirt] [PATCH] Fix mem leak in virQEMUCapsProbeQMPMachineTypes on OOM
Michal Privoznik
mprivozn at redhat.com
Mon Nov 11 12:28:04 UTC 2013
On 11.11.2013 12:15, Daniel P. Berrange wrote:
> From: "Daniel P. Berrange" <berrange at redhat.com>
>
> 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 at redhat.com>
> ---
> src/qemu/qemu_capabilities.c | 11 +++++++----
> 1 file changed, 7 insertions(+), 4 deletions(-)
>
> diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c
> index 71a913b..2712a4d 100644
> --- a/src/qemu/qemu_capabilities.c
> +++ b/src/qemu/qemu_capabilities.c
> @@ -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;
Wouldn't be sufficient to move this line ^^^ before the for() loop?
Something like we already have in virQEMUCapsNewCopy().
At any rate, this one's good too. ACK.
Michal
More information about the libvir-list
mailing list