[PATCH] qemuDomainSaveInternal: fix memoryleak of virDomainDef

Chuan Zheng posted 1 patch 2 weeks ago
Test syntax-check failed
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/libvirt tags/patchew/1595987766-97633-1-git-send-email-zhengchuan@huawei.com
src/qemu/qemu_driver.c | 1 +
1 file changed, 1 insertion(+)

[PATCH] qemuDomainSaveInternal: fix memoryleak of virDomainDef

Posted by Chuan Zheng 2 weeks ago
From: Zheng Chuan <zhengchuan@huawei.com>

virDomainDefPtr 'def' is forgot to free after qemuDomainDefFormatLive(), fix it.

Signed-off-by: Zheng Chuan <zhengchuan@huawei.com>
---
 src/qemu/qemu_driver.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index 53980d4..b145318 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -3369,6 +3369,7 @@ qemuDomainSaveInternal(virQEMUDriverPtr driver,
             goto endjob;
         }
         xml = qemuDomainDefFormatLive(driver, priv->qemuCaps, def, NULL, true, true);
+        virDomainDefFree(def);
     } else {
         xml = qemuDomainDefFormatLive(driver, priv->qemuCaps, vm->def,
                                       priv->origCPU, true, true);
-- 
1.8.3.1

Re: [PATCH] qemuDomainSaveInternal: fix memoryleak of virDomainDef

Posted by Laine Stump 2 weeks ago
On 7/28/20 9:56 PM, Chuan Zheng wrote:
> From: Zheng Chuan <zhengchuan@huawei.com>
>
> virDomainDefPtr 'def' is forgot to free after qemuDomainDefFormatLive(), fix it.
>
> Signed-off-by: Zheng Chuan <zhengchuan@huawei.com>
> ---
>   src/qemu/qemu_driver.c | 1 +
>   1 file changed, 1 insertion(+)
>
> diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
> index 53980d4..b145318 100644
> --- a/src/qemu/qemu_driver.c
> +++ b/src/qemu/qemu_driver.c
> @@ -3369,6 +3369,7 @@ qemuDomainSaveInternal(virQEMUDriverPtr driver,
>               goto endjob;
>           }
>           xml = qemuDomainDefFormatLive(driver, priv->qemuCaps, def, NULL, true, true);
> +        virDomainDefFree(def);
>       } else {
>           xml = qemuDomainDefFormatLive(driver, priv->qemuCaps, vm->def,
>                                         priv->origCPU, true, true);

Instead you could use g_autoptr when defining def:


   g_autoptr(virDomainDef) def = NULL;


(and then remove the existing call to virDomainDefFree() that's a few 
lines down)(and get rid of the {  } around the now single line "goto 
endjob;" that remains). That way def is just always freed when leaving 
the scope of the "if (xmlin) { }"