qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* Fix a potential memory leak bug in write_boot_rom() (v6.2.0).
@ 2022-02-23 14:39 wliang
  2022-02-23 16:15 ` Philippe Mathieu-Daudé
  0 siblings, 1 reply; 5+ messages in thread
From: wliang @ 2022-02-23 14:39 UTC (permalink / raw)
  To: qemu-devel@nongnu.org


[-- Attachment #1.1: Type: text/plain, Size: 1004 bytes --]

Hi all,

I find a memory leak bug in QEMU 6.2.0, which is in write_boot_rom()(./hw/arm/aspeed.c).

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


259    BlockBackend *blk = blk_by_legacy_dinfo(dinfo);
...
276    storage = g_new0(uint8_t, rom_size);
277    if (blk_pread(blk, 0, storage, rom_size) < 0) {
278        error_setg(errp, "failed to read the initial flash content");
279        return;
280    }


I believe that the problem can be fixed by adding a g_free() before the function returns.


277    if (blk_pread(blk, 0, storage, rom_size) < 0) {
278        error_setg(errp, "failed to read the initial flash content");
+++    g_free(storage);
279        return;
280    }


I'm looking forward to your confirmation.

Best,
Wentao

[-- Attachment #1.2: Type: text/html, Size: 5048 bytes --]

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: aspeed.c.patch --]
[-- Type: text/x-patch; name=aspeed.c.patch, Size: 360 bytes --]

--- ./hw/arm/aspeed.c	2022-02-23 15:06:31.928708083 +0800
+++ ./hw/arm/aspeed-PATCH.c	2022-02-23 21:22:28.200802801 +0800
@@ -276,6 +276,7 @@
     storage = g_new0(uint8_t, rom_size);
     if (blk_pread(blk, 0, storage, rom_size) < 0) {
         error_setg(errp, "failed to read the initial flash content");
+        g_free(storage);
         return;
     }
 

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

end of thread, other threads:[~2022-02-25 11:42 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-02-23 14:39 Fix a potential memory leak bug in write_boot_rom() (v6.2.0) wliang
2022-02-23 16:15 ` Philippe Mathieu-Daudé
2022-02-24  8:10   ` Cédric Le Goater
2022-02-25  3:30     ` wliang
2022-02-25 11:37       ` 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).