[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [PATCH] hw/loongarch/virt: Fix memory leak
From: |
Peter Maydell |
Subject: |
Re: [PATCH] hw/loongarch/virt: Fix memory leak |
Date: |
Wed, 8 May 2024 14:05:21 +0100 |
On Tue, 7 May 2024 at 10:52, Michael Tokarev <mjt@tls.msk.ru> wrote:
>
> 07.05.2024 05:22, Song Gao wrote:
>
> > for (i = 1; i < nb_numa_nodes; i++) {
> > MemoryRegion *nodemem = g_new(MemoryRegion, 1);
> > - ramName = g_strdup_printf("loongarch.node%d.ram", i);
> > + g_autofree char *ramName = g_strdup_printf("loongarch.node%d.ram",
> > i);
>
> Can't this be a fixed-size buffer on stack?
No, this is a really bad idea. It's a pain to audit that the
array really doesn't get overwritten, and if the string we want
to write changes, now we have to re-count characters to decide
if we need to increase the size of the array. The memory allocation
on the heap here is a tiny overhead that we only incur at startup.
The g_autofree approach is much better.
For this version of the patch:
Reviewed-by: Peter Maydell <peter.maydell@linaro.org>
thanks
-- PMM