Fix: autodisc: make sure auto_discover_source_for_input_as_dir_or_file_rec closes dir
authorSimon Marchi <simon.marchi@efficios.com>
Wed, 27 Jul 2022 03:31:06 +0000 (23:31 -0400)
committerPhilippe Proulx <eeppeliteloop@gmail.com>
Mon, 1 Aug 2022 18:43:31 +0000 (14:43 -0400)
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 <simon.marchi@efficios.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/8612
Tested-by: jenkins <jenkins@lttng.org>
Reviewed-by: Philippe Proulx <eeppeliteloop@gmail.com>
src/autodisc/autodisc.c

index 1a4f07dc39d537011f01428cb1d120d34dd02208..0ae53817866924f16e74b0da9b471ccb715f1c01 100644 (file)
@@ -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);
This page took 0.026803 seconds and 4 git commands to generate.