Skip to content

Commit

Permalink
btrfs-progs: cmd/qgroup: fix memory leak for clear-stale subcommand
Browse files Browse the repository at this point in the history
[BUG]
ASAN test fails at misc/055 with the following leak:

Qgroupid    Referenced    Exclusive   Path
--------    ----------    ---------   ----
0/5           16.00KiB     16.00KiB   <toplevel>
0/256         16.00KiB     16.00KiB   <stale>
====== RUN CHECK /home/runner/work/btrfs-progs/btrfs-progs/btrfs qgroup clear-stale /home/runner/work/btrfs-progs/btrfs-progs/tests/mnt

=================================================================
==102571==ERROR: LeakSanitizer: detected memory leaks

Indirect leak of 4096 byte(s) in 1 object(s) allocated from:
    #0 0x7fd1c98fbb37 in malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:69
    #1 0x55aa2f8953f8 in btrfs_util_subvolume_path_fd libbtrfsutil/subvolume.c:178
    kdave#2 0x55aa2f8fa2a6 in get_or_add_qgroup cmds/qgroup.c:837
    kdave#3 0x55aa2f8fa7e9 in update_qgroup_info cmds/qgroup.c:883
    kdave#4 0x55aa2f8fd912 in __qgroups_search cmds/qgroup.c:1385
    kdave#5 0x55aa2f8fe196 in qgroups_search_all cmds/qgroup.c:1453
    kdave#6 0x55aa2f902a7c in cmd_qgroup_clear_stale cmds/qgroup.c:2281
    kdave#7 0x55aa2f73425b in cmd_execute cmds/commands.h:126
    kdave#8 0x55aa2f734bcc in handle_command_group /home/runner/work/btrfs-progs/btrfs-progs/btrfs.c:177
    kdave#9 0x55aa2f73425b in cmd_execute cmds/commands.h:126
    kdave#10 0x55aa2f735a96 in main /home/runner/work/btrfs-progs/btrfs-progs/btrfs.c:518
    kdave#11 0x7fd1c942a1c9  (/lib/x86_64-linux-gnu/libc.so.6+0x2a1c9) (BuildId: 08134323d00289185684a4cd177d202f39c2a5f3)
    kdave#12 0x7fd1c942a28a in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2a28a) (BuildId: 08134323d00289185684a4cd177d202f39c2a5f3)
    kdave#13 0x55aa2f734144 in _start (/home/runner/work/btrfs-progs/btrfs-progs/btrfs+0x84144) (BuildId: 56f3dd838e1ae189c142c5d27fac025cd46deddb)

Indirect leak of 432 byte(s) in 2 object(s) allocated from:
    #0 0x7fd1c98fb4d0 in calloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:77
    #1 0x55aa2f8fa1a1 in get_or_add_qgroup cmds/qgroup.c:822
    kdave#2 0x55aa2f8fa7e9 in update_qgroup_info cmds/qgroup.c:883
    kdave#3 0x55aa2f8fd912 in __qgroups_search cmds/qgroup.c:1385
    kdave#4 0x55aa2f8fe196 in qgroups_search_all cmds/qgroup.c:1453
    kdave#5 0x55aa2f902a7c in cmd_qgroup_clear_stale cmds/qgroup.c:2281
    kdave#6 0x55aa2f73425b in cmd_execute cmds/commands.h:126
    kdave#7 0x55aa2f734bcc in handle_command_group /home/runner/work/btrfs-progs/btrfs-progs/btrfs.c:177
    kdave#8 0x55aa2f73425b in cmd_execute cmds/commands.h:126
    kdave#9 0x55aa2f735a96 in main /home/runner/work/btrfs-progs/btrfs-progs/btrfs.c:518
    kdave#10 0x7fd1c942a1c9  (/lib/x86_64-linux-gnu/libc.so.6+0x2a1c9) (BuildId: 08134323d00289185684a4cd177d202f39c2a5f3)
    kdave#11 0x7fd1c942a28a in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2a28a) (BuildId: 08134323d00289185684a4cd177d202f39c2a5f3)
    kdave#12 0x55aa2f734144 in _start (/home/runner/work/btrfs-progs/btrfs-progs/btrfs+0x84144) (BuildId: 56f3dd838e1ae189c142c5d27fac025cd46deddb)

[CAUSE]
Above leaks are caused by two btrfs_qgroup structures and one path for
toplevel qgroup.

It's caused by the fact that we called qgroups_search_all() but didn't
do any cleanup.

[FIX]
Call __free_all_qgroups() inside cmd_qgroup_clear_stale() to properly
free the qgroups.

Fixes: 701ab15 ("btrfs-progs: qgroup: new command to delete stale qgroups")
Signed-off-by: Qu Wenruo <wqu@suse.com>
  • Loading branch information
adam900710 committed Jul 29, 2024
1 parent 9ec04f2 commit 9132fa0
Showing 1 changed file with 1 addition and 0 deletions.
1 change: 1 addition & 0 deletions cmds/qgroup.c
Original file line number Diff line number Diff line change
Expand Up @@ -2301,6 +2301,7 @@ static int cmd_qgroup_clear_stale(const struct cmd_struct *cmd, int argc, char *

out:
close(fd);
__free_all_qgroups(&qgroup_lookup);
return !!ret;
}
static DEFINE_SIMPLE_COMMAND(qgroup_clear_stale, "clear-stale");
Expand Down

0 comments on commit 9132fa0

Please sign in to comment.