qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Philippe Mathieu-Daudé" <philippe.mathieu.daude@gmail.com>
To: wliang@stu.xidian.edu.cn,
	"qemu-devel@nongnu.org" <qemu-devel@nongnu.org>,
	"Cédric Le Goater" <clg@kaod.org>
Subject: Re: Fix a potential memory leak bug in write_boot_rom() (v6.2.0).
Date: Wed, 23 Feb 2022 17:15:26 +0100	[thread overview]
Message-ID: <c91392a4-f75b-4e5d-9e6c-04777fb7ca79@gmail.com> (raw)
In-Reply-To: <6e7748f1.25d8.17f2705c420.Coremail.wliang@stu.xidian.edu.cn>

On 23/2/22 15:39, wliang@stu.xidian.edu.cn wrote:
> 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.

Correct.

Or using g_autofree:

-- >8 --
diff --git a/hw/arm/aspeed.c b/hw/arm/aspeed.c
index d911dc904f..170e773ef8 100644
--- a/hw/arm/aspeed.c
+++ b/hw/arm/aspeed.c
@@ -257,7 +257,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
@@ -273,14 +273,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);
  }
---


  reply	other threads:[~2022-02-23 16:18 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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é [this message]
2022-02-24  8:10   ` Cédric Le Goater
2022-02-25  3:30     ` wliang
2022-02-25 11:37       ` Peter Maydell

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=c91392a4-f75b-4e5d-9e6c-04777fb7ca79@gmail.com \
    --to=philippe.mathieu.daude@gmail.com \
    --cc=clg@kaod.org \
    --cc=qemu-devel@nongnu.org \
    --cc=wliang@stu.xidian.edu.cn \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).