Linux-BTRFS Archive mirror
 help / color / mirror / Atom feed
From: Nathan Mills <the.true.nathan.mills@gmail.com>
To: linux-btrfs@vger.kernel.org
Subject: Inspect command leaks memory if realloc() fails
Date: Fri, 5 Apr 2024 08:55:00 -0700	[thread overview]
Message-ID: <CALjJ9cdoxY7E4omFtXcZigNV9Q8_+q95do=b=PrzsN+JbBK23Q@mail.gmail.com> (raw)

In `cmd_inspect_list_chunks()` in Btrfs v6.8-g3793e987 on the master
branch, there is this code on line 1167:
```c
ctx.stats = realloc(ctx.stats, ctx.size * sizeof(ctx.stats[0]));
```

This is a "common realloc mistake" according to CppCheck. The fix is
to assign the result of realloc to a temporary variable and only
assign the original variable if the temp is non-NULL. That way we
don't lose the pointer to the old memory.

I found this by grepping the source for this regex:
`([A-Za-z_0-9->\.]+)\s*=\s*realloc\s*\(\s*\1` after noticing a
CppCheck warning for a different part of btrfs that already uses a
temporary to hold the reallocated memory.

Here's a patch to fix it (note: this patch probably won't apply to
devel because the goto label has been changed in `v6.8-19-g512fa7e6`):

commit 7f65051ee07a46a9cb7e0aa0134af0cc77db0a16 (HEAD ->
realloc-memory-leak, myfork/realloc-memory-leak)
Author: Nathan Mills <38995150+Quipyowert2@users.noreply.github.com>
Date:   Thu Apr 4 19:34:28 2024 -0700

    btrfs-progs: cmds: fix inspect memory leak.

diff --git a/cmds/inspect.c b/cmds/inspect.c
index 145196d0..e665a281 100644
--- a/cmds/inspect.c
+++ b/cmds/inspect.c
@@ -1162,12 +1162,15 @@ static int cmd_inspect_list_chunks(const
struct cmd_struct *cmd,

                                if (ctx.length == ctx.size) {
                                        ctx.size += 1024;
-                                       ctx.stats = realloc(ctx.stats, ctx.size
+                                       struct list_chunks_entry *tmp;
+                                       tmp = realloc(ctx.stats, ctx.size
                                                * sizeof(ctx.stats[0]));
-                                       if (!ctx.stats) {
+                                       if (!tmp) {
                                                ret = 1;

error_msg(ERROR_MSG_MEMORY, NULL);
                                                goto out_nomem;
+                                       } else {
+                                               ctx.stats = tmp;
                                        }
                                }
                        }

                 reply	other threads:[~2024-04-05 15:55 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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='CALjJ9cdoxY7E4omFtXcZigNV9Q8_+q95do=b=PrzsN+JbBK23Q@mail.gmail.com' \
    --to=the.true.nathan.mills@gmail.com \
    --cc=linux-btrfs@vger.kernel.org \
    /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 read-only IMAP folder(s) and NNTP newsgroup(s).