btrfs-progs/cmds
Qu Wenruo 5aed7ea89a btrfs-progs: qgroup clear-stale: fix memory leak
[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
    #2 0x55aa2f8fa2a6 in get_or_add_qgroup cmds/qgroup.c:837
    #3 0x55aa2f8fa7e9 in update_qgroup_info cmds/qgroup.c:883
    #4 0x55aa2f8fd912 in __qgroups_search cmds/qgroup.c:1385
    #5 0x55aa2f8fe196 in qgroups_search_all cmds/qgroup.c:1453
    #6 0x55aa2f902a7c in cmd_qgroup_clear_stale cmds/qgroup.c:2281
    #7 0x55aa2f73425b in cmd_execute cmds/commands.h:126
    #8 0x55aa2f734bcc in handle_command_group /home/runner/work/btrfs-progs/btrfs-progs/btrfs.c:177
    #9 0x55aa2f73425b in cmd_execute cmds/commands.h:126
    #10 0x55aa2f735a96 in main /home/runner/work/btrfs-progs/btrfs-progs/btrfs.c:518
    #11 0x7fd1c942a1c9  (/lib/x86_64-linux-gnu/libc.so.6+0x2a1c9) (BuildId: 08134323d00289185684a4cd177d202f39c2a5f3)
    #12 0x7fd1c942a28a in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2a28a) (BuildId: 08134323d00289185684a4cd177d202f39c2a5f3)
    #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
    #2 0x55aa2f8fa7e9 in update_qgroup_info cmds/qgroup.c:883
    #3 0x55aa2f8fd912 in __qgroups_search cmds/qgroup.c:1385
    #4 0x55aa2f8fe196 in qgroups_search_all cmds/qgroup.c:1453
    #5 0x55aa2f902a7c in cmd_qgroup_clear_stale cmds/qgroup.c:2281
    #6 0x55aa2f73425b in cmd_execute cmds/commands.h:126
    #7 0x55aa2f734bcc in handle_command_group /home/runner/work/btrfs-progs/btrfs-progs/btrfs.c:177
    #8 0x55aa2f73425b in cmd_execute cmds/commands.h:126
    #9 0x55aa2f735a96 in main /home/runner/work/btrfs-progs/btrfs-progs/btrfs.c:518
    #10 0x7fd1c942a1c9  (/lib/x86_64-linux-gnu/libc.so.6+0x2a1c9) (BuildId: 08134323d00289185684a4cd177d202f39c2a5f3)
    #11 0x7fd1c942a28a in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2a28a) (BuildId: 08134323d00289185684a4cd177d202f39c2a5f3)
    #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: 701ab151c2 ("btrfs-progs: qgroup: new command to delete stale qgroups")
Signed-off-by: Qu Wenruo <wqu@suse.com>
2024-07-30 20:04:50 +02:00
..
balance.c btrfs-progs: drop _fd from btrfs_open_dir_fd() 2024-02-20 12:56:24 +01:00
commands.h btrfs-progs: use unsigned type for bit shift values 2023-11-03 18:04:37 +01:00
device.c btrfs-progs: use strncpy_null everywhere 2024-06-24 19:18:48 +02:00
filesystem-du.c btrfs-progs: rename btrfs_open_fd2() to btrfs_open_path() 2024-02-20 12:59:29 +01:00
filesystem-usage.c btrfs-progs: reorder key initializations 2024-04-30 21:49:15 +02:00
filesystem-usage.h btrfs-progs: convert device info to struct array 2023-10-17 19:34:00 +02:00
filesystem.c btrfs-progs: run codespell throughout fixing typos automagically 2024-07-30 19:56:08 +02:00
inspect-dump-super.c btrfs-progs: clean up includes, using include-what-you-use 2023-10-03 01:11:57 +02:00
inspect-dump-tree.c btrfs-progs: reorder key initializations 2024-04-30 21:49:15 +02:00
inspect-tree-stats.c btrfs-progs: tree-stats: unify setting raw/bytes options 2024-07-30 19:53:33 +02:00
inspect.c btrfs-progs: use proper path buffer in __ino_to_path_fd() 2024-07-30 20:04:38 +02:00
property.c btrfs-progs: property set: fix typo in help message 2024-04-30 19:34:52 +02:00
props.h btrfs-progs: use unsigned type for bit shift values 2023-11-03 18:04:37 +01:00
qgroup.c btrfs-progs: qgroup clear-stale: fix memory leak 2024-07-30 20:04:50 +02:00
qgroup.h btrfs-progs: qgroups: remove support for num_ref_copies/num_excl_copies 2024-03-04 22:55:07 +01:00
quota.c btrfs-progs: drop _fd from btrfs_open_dir_fd() 2024-02-20 12:56:24 +01:00
receive-dump.c btrfs-progs: factor string escaping helpers from receive dump 2024-07-30 19:53:33 +02:00
receive-dump.h btrfs-progs: cmds: update include lists 2022-10-11 09:07:59 +02:00
receive.c btrfs-progs: rename and move __strncpy_null to string-utils 2024-06-24 19:18:46 +02:00
reflink.c btrfs-progs: implement arg_strtou64_with_suffix() with a new helper 2024-01-18 02:14:23 +01:00
replace.c btrfs-progs: use strncpy_null everywhere 2024-06-24 19:18:48 +02:00
rescue-chunk-recover.c btrfs-progs: reorder key initializations 2024-04-30 21:49:15 +02:00
rescue-super-recover.c btrfs-progs: clean up includes, using include-what-you-use 2023-10-03 01:11:57 +02:00
rescue.c btrfs-progs: rescue: properly close the fs for clear-ino-cache 2024-02-08 08:30:37 +01:00
rescue.h btrfs-progs: unify GPL header comments 2021-09-07 13:58:44 +02:00
restore.c btrfs-progs: use strncpy_null everywhere 2024-06-24 19:18:48 +02:00
scrub.c btrfs-progs: scrub status: with --si, show rate in metric units 2024-07-30 19:53:32 +02:00
send.c btrfs-progs: use get_sysfs_proto_supported() in get_sysfs_proto_supported() 2023-12-01 01:58:56 +01:00
subvolume-list.c btrfs-progs: subvol list: fix accidental trimming of subvolume name 2024-06-25 17:42:12 +02:00
subvolume.c btrfs-progs: rename and move __strncpy_null to string-utils 2024-06-24 19:18:46 +02:00
subvolume.h btrfs-progs: subvol: introduce rowspec definition for json output 2023-08-28 17:24:23 +02:00