From d2b44fef4f0e01f43783115f7c2e97e32c970a52 Mon Sep 17 00:00:00 2001 From: Simon Marchi Date: Tue, 26 Jul 2022 23:31:06 -0400 Subject: [PATCH] Fix: autodisc: make sure auto_discover_source_for_input_as_dir_or_file_rec closes dir The `dir` variable does not get released if the code goes to the `end` or `error` labels, leaking the GDir. Move the variable to the function scope and release it before returning. Fixes this, found by Valgrind: ==2456047== 32,824 (8 direct, 32,816 indirect) bytes in 1 blocks are definitely lost in loss record 36 of 37 ==2456047== at 0x4845888: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==2456047== by 0x49A9B19: g_malloc (gmem.c:125) ==2456047== by 0x49BEDD4: UnknownInlinedFun (gstrfuncs.c:424) ==2456047== by 0x49BEDD4: g_memdup2 (gstrfuncs.c:417) ==2456047== by 0x498D0D7: UnknownInlinedFun (gdir.c:117) ==2456047== by 0x498D0D7: g_dir_open (gdir.c:145) ==2456047== by 0x12BE5A: auto_discover_source_for_input_as_dir_or_file_rec (autodisc.c:613) ==2456047== by 0x12BFEC: auto_discover_source_for_input_as_dir_or_file_rec (autodisc.c:639) ==2456047== by 0x12BFEC: auto_discover_source_for_input_as_dir_or_file_rec (autodisc.c:639) ==2456047== by 0x12BFEC: auto_discover_source_for_input_as_dir_or_file_rec (autodisc.c:639) ==2456047== by 0x12C1B9: auto_discover_source_for_input_as_dir_or_file (autodisc.c:710) ==2456047== by 0x12C2BA: auto_discover_source_components (autodisc.c:753) ==2456047== by 0x1247A6: bt_config_convert_from_args (babeltrace2-cfg-cli-args.c:4270) ==2456047== by 0x1262B7: bt_config_cli_args_create (babeltrace2-cfg-cli-args.c:4951) Change-Id: I2145d588c80fb47b5f3475a6a68cc67bb683edb6 Signed-off-by: Simon Marchi Reviewed-on: https://review.lttng.org/c/babeltrace/+/8612 Tested-by: jenkins Reviewed-by: Philippe Proulx --- src/autodisc/autodisc.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/autodisc/autodisc.c b/src/autodisc/autodisc.c index 1a4f07dc..0ae53817 100644 --- a/src/autodisc/autodisc.c +++ b/src/autodisc/autodisc.c @@ -579,6 +579,7 @@ auto_source_discovery_internal_status auto_discover_source_for_input_as_dir_or_f { auto_source_discovery_internal_status status; GError *error = NULL; + GDir *dir = NULL; if (g_file_test(input->str, G_FILE_TEST_IS_REGULAR)) { /* It's a file. */ @@ -587,7 +588,6 @@ auto_source_discovery_internal_status auto_discover_source_for_input_as_dir_or_f component_class_restrict, log_level, auto_disc, interrupter); } else if (g_file_test(input->str, G_FILE_TEST_IS_DIR)) { - GDir *dir; const gchar *dirent; gsize saved_input_len; int dir_status = AUTO_SOURCE_DISCOVERY_INTERNAL_STATUS_NO_MATCH; @@ -658,8 +658,6 @@ auto_source_discovery_internal_status auto_discover_source_for_input_as_dir_or_f } while (dirent); status = dir_status; - - g_dir_close(dir); } else { BT_LOGD("Skipping %s, not a file or directory", input->str); status = AUTO_SOURCE_DISCOVERY_INTERNAL_STATUS_NO_MATCH; @@ -671,6 +669,9 @@ error: status = AUTO_SOURCE_DISCOVERY_INTERNAL_STATUS_ERROR; end: + if (dir) { + g_dir_close(dir); + } if (error) { g_error_free(error); -- 2.34.1