Skip to contentSkip to conversation

aspeed: Fix a potential memory leak bug in write_boot_rom()

PATCH In review Cédric Le Goater 2 participants 2 messages Mar 2, 2022 Arm/Aspeed machine support

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 aspeed: Fix a potential memory leak bug in write_boot_rom()

From: Wentao_Liang <[email protected]>

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.

---
 hw/arm/aspeed.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
hw/arm/aspeed.c +2 -3
index 11558b327bc9..b71bc2559baa 100644
@@ -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,
Signature · 1 line
2.34.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Cédric Le Goater

From: Wentao_Liang <[email protected]>

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.

Quoted 6 lines

Use g_autofree to fix the issue.

thanks
-- PMM