[PATCH] hw/loongarch/virt.c: Fixes memory leak in ramName during loop iterations

R.Samarasekara posted 1 patch 6 months, 2 weeks ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20240509132802.247147-1-rsamarnz@gmail.com
Maintainers: Song Gao <gaosong@loongson.cn>
hw/loongarch/virt.c | 1 +
1 file changed, 1 insertion(+)
[PATCH] hw/loongarch/virt.c: Fixes memory leak in ramName during loop iterations
Posted by R.Samarasekara 6 months, 2 weeks ago
This patch fixes a memory leak in the ramName variable within the
hw/loongarch/virt.c file. The leak occurs due to repeated calls to
g_strdup_printf within a loop, causing memory allocated for ramName on
previous iterations to be unfreed.

Signed-off-by: R.Samarasekara <rsamarnz@gmail.com>
---
 hw/loongarch/virt.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/hw/loongarch/virt.c b/hw/loongarch/virt.c
index c0999878df..1fe02f8501 100644
--- a/hw/loongarch/virt.c
+++ b/hw/loongarch/virt.c
@@ -954,6 +954,7 @@ static void loongarch_init(MachineState *machine)
         fdt_add_memory_node(machine, phyAddr, numa_info[i].node_mem, i);
         offset += numa_info[i].node_mem;
         phyAddr += numa_info[i].node_mem;
+        g_free(ramName);
     }
 
     /* initialize device memory address space */
-- 
2.40.1
Re: [PATCH] hw/loongarch/virt.c: Fixes memory leak in ramName during loop iterations
Posted by Philippe Mathieu-Daudé 6 months, 2 weeks ago
Hi R.

On 9/5/24 15:28, R.Samarasekara wrote:
> This patch fixes a memory leak in the ramName variable within the
> hw/loongarch/virt.c file. The leak occurs due to repeated calls to
> g_strdup_printf within a loop, causing memory allocated for ramName on
> previous iterations to be unfreed.
> 
> Signed-off-by: R.Samarasekara <rsamarnz@gmail.com>
> ---
>   hw/loongarch/virt.c | 1 +
>   1 file changed, 1 insertion(+)
> 
> diff --git a/hw/loongarch/virt.c b/hw/loongarch/virt.c
> index c0999878df..1fe02f8501 100644
> --- a/hw/loongarch/virt.c
> +++ b/hw/loongarch/virt.c
> @@ -954,6 +954,7 @@ static void loongarch_init(MachineState *machine)
>           fdt_add_memory_node(machine, phyAddr, numa_info[i].node_mem, i);
>           offset += numa_info[i].node_mem;
>           phyAddr += numa_info[i].node_mem;
> +        g_free(ramName);
>       }

This has been fixed recently in this commit:
https://gitlab.com/qemu-project/qemu/-/commit/54c52ec719

Regards,

Phil.