[PATCH] qemu: Avoid memory leak in virQEMUCapsCPUDefsToModels

Michal Privoznik posted 1 patch 1 year, 5 months ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/libvirt tags/patchew/48173a5de02bdf70a45cd13753f62a4a1f5769c9.1665409175.git.mprivozn@redhat.com
src/qemu/qemu_capabilities.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
[PATCH] qemu: Avoid memory leak in virQEMUCapsCPUDefsToModels
Posted by Michal Privoznik 1 year, 5 months ago
The @vendor variable inside virQEMUCapsCPUDefsToModels() is
allocated, but never freed. But there is actually no need for it
to be allocated, because it merely passes a retval of
virCPUGetVendorForModel() (which returns a const string) to
virDomainCapsCPUModelsAdd() (which ten accepts the argument as
const string). Therefore, drop the g_strdup() call and fix the
type of the variable.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
---
 src/qemu/qemu_capabilities.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c
index 6f3ff7f43f..04b2eef215 100644
--- a/src/qemu/qemu_capabilities.c
+++ b/src/qemu/qemu_capabilities.c
@@ -2189,7 +2189,7 @@ virQEMUCapsCPUDefsToModels(virArch arch,
 
     for (i = 0; i < defs->ncpus; i++) {
         qemuMonitorCPUDefInfo *cpu = defs->cpus + i;
-        char *vendor = NULL;
+        const char *vendor = NULL;
 
         if (modelAllowed && !g_strv_contains(modelAllowed, cpu->name))
             continue;
@@ -2198,7 +2198,7 @@ virQEMUCapsCPUDefsToModels(virArch arch,
             continue;
 
         if (vendors)
-            vendor = g_strdup(virCPUGetVendorForModel(arch, cpu->name));
+            vendor = virCPUGetVendorForModel(arch, cpu->name);
 
         virDomainCapsCPUModelsAdd(cpuModels, cpu->name, cpu->usable,
                                   cpu->blockers, cpu->deprecated, vendor);
-- 
2.35.1
Re: [PATCH] qemu: Avoid memory leak in virQEMUCapsCPUDefsToModels
Posted by Jiri Denemark 1 year, 5 months ago
On Mon, Oct 10, 2022 at 15:39:35 +0200, Michal Privoznik wrote:
> The @vendor variable inside virQEMUCapsCPUDefsToModels() is
> allocated, but never freed. But there is actually no need for it
> to be allocated, because it merely passes a retval of
> virCPUGetVendorForModel() (which returns a const string) to
> virDomainCapsCPUModelsAdd() (which ten accepts the argument as
> const string). Therefore, drop the g_strdup() call and fix the
> type of the variable.
> 
> Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
> ---
>  src/qemu/qemu_capabilities.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c
> index 6f3ff7f43f..04b2eef215 100644
> --- a/src/qemu/qemu_capabilities.c
> +++ b/src/qemu/qemu_capabilities.c
> @@ -2189,7 +2189,7 @@ virQEMUCapsCPUDefsToModels(virArch arch,
>  
>      for (i = 0; i < defs->ncpus; i++) {
>          qemuMonitorCPUDefInfo *cpu = defs->cpus + i;
> -        char *vendor = NULL;
> +        const char *vendor = NULL;
>  
>          if (modelAllowed && !g_strv_contains(modelAllowed, cpu->name))
>              continue;
> @@ -2198,7 +2198,7 @@ virQEMUCapsCPUDefsToModels(virArch arch,
>              continue;
>  
>          if (vendors)
> -            vendor = g_strdup(virCPUGetVendorForModel(arch, cpu->name));
> +            vendor = virCPUGetVendorForModel(arch, cpu->name);
>  
>          virDomainCapsCPUModelsAdd(cpuModels, cpu->name, cpu->usable,
>                                    cpu->blockers, cpu->deprecated, vendor);

Oops, right g_strdup is called on vendor in virDomainCapsCPUModelsAdd.

Reviewed-by: Jiri Denemark <jdenemar@redhat.com>