qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] aspeed: Fix a potential memory leak bug in write_boot_rom()
@ 2022-03-02  8:33 Cédric Le Goater
  2022-03-02 12:29 ` Peter Maydell
  0 siblings, 1 reply; 2+ messages in thread
From: Cédric Le Goater @ 2022-03-02  8:33 UTC (permalink / raw)
  To: qemu-arm, qemu-devel
  Cc: Peter Maydell, Wentao_Liang, Andrew Jeffery,
	Philippe Mathieu-Daudé, Cédric Le Goater, Joel Stanley

From: Wentao_Liang <Wentao_Liang_g@163.com>

A memory chunk is allocated with g_new0() and assigned to the variable
'storage'. However, if the branch takes true, there will be only an
error report but not a free operation for 'storage' before function
returns. As a result, a memory leak bug is triggered.

Use g_autofree to fix the issue.

Suggested-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
Signed-off-by: Wentao_Liang <Wentao_Liang_g@163.com>
[ clg: reworked the commit log ]
Signed-off-by: Cédric Le Goater <clg@kaod.org>
---
 hw/arm/aspeed.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/hw/arm/aspeed.c b/hw/arm/aspeed.c
index 11558b327bc9..b71bc2559baa 100644
--- a/hw/arm/aspeed.c
+++ b/hw/arm/aspeed.c
@@ -246,7 +246,7 @@ static void write_boot_rom(DriveInfo *dinfo, hwaddr addr, size_t rom_size,
                            Error **errp)
 {
     BlockBackend *blk = blk_by_legacy_dinfo(dinfo);
-    uint8_t *storage;
+    g_autofree void *storage = NULL;
     int64_t size;
 
     /* The block backend size should have already been 'validated' by
@@ -262,14 +262,13 @@ static void write_boot_rom(DriveInfo *dinfo, hwaddr addr, size_t rom_size,
         rom_size = size;
     }
 
-    storage = g_new0(uint8_t, rom_size);
+    storage = g_malloc0(rom_size);
     if (blk_pread(blk, 0, storage, rom_size) < 0) {
         error_setg(errp, "failed to read the initial flash content");
         return;
     }
 
     rom_add_blob_fixed("aspeed.boot_rom", storage, rom_size, addr);
-    g_free(storage);
 }
 
 static void aspeed_board_init_flashes(AspeedSMCState *s,
-- 
2.34.1



^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] aspeed: Fix a potential memory leak bug in write_boot_rom()
  2022-03-02  8:33 [PATCH] aspeed: Fix a potential memory leak bug in write_boot_rom() Cédric Le Goater
@ 2022-03-02 12:29 ` Peter Maydell
  0 siblings, 0 replies; 2+ messages in thread
From: Peter Maydell @ 2022-03-02 12:29 UTC (permalink / raw)
  To: Cédric Le Goater
  Cc: Wentao_Liang, Andrew Jeffery, qemu-devel,
	Philippe Mathieu-Daudé, qemu-arm, Joel Stanley

On Wed, 2 Mar 2022 at 08:33, Cédric Le Goater <clg@kaod.org> wrote:
>
> From: Wentao_Liang <Wentao_Liang_g@163.com>
>
> A memory chunk is allocated with g_new0() and assigned to the variable
> 'storage'. However, if the branch takes true, there will be only an
> error report but not a free operation for 'storage' before function
> returns. As a result, a memory leak bug is triggered.
>
> Use g_autofree to fix the issue.
>
> Suggested-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
> Signed-off-by: Wentao_Liang <Wentao_Liang_g@163.com>
> [ clg: reworked the commit log ]
> Signed-off-by: Cédric Le Goater <clg@kaod.org>

Reviewed-by: Peter Maydell <peter.maydell@linaro.org>

thanks
-- PMM


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-03-02 13:48 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-03-02  8:33 [PATCH] aspeed: Fix a potential memory leak bug in write_boot_rom() Cédric Le Goater
2022-03-02 12:29 ` Peter Maydell

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).