cli: free log level string value
authorSimon Marchi <simon.marchi@efficios.com>
Tue, 7 Jan 2020 19:23:47 +0000 (14:23 -0500)
committerSimon Marchi <simon.marchi@efficios.com>
Wed, 8 Jan 2020 18:07:04 +0000 (18:07 +0000)
In bt_config_convert_from_args, when encoutering a --log-level argument
applied to a non-option argument, we create a string bt_value.  We add
it to an array, which takes its own reference on it.  However, in the
successful case, we never drop our reference to it, so it's never freed.
Fix it by calling bt_value_put_ref on it in all cases.

This is the error reported by address sanitizer:

Direct leak of 128 byte(s) in 2 object(s) allocated from:
    #0 0x7f1e36724d38 in __interceptor_calloc (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xded38)
    #1 0x7f1e35d97b10 in g_malloc0 (/usr/lib/x86_64-linux-gnu/libglib-2.0.so.0+0x51b10)
    #2 0x558c39c19d3b in bt_config_convert_from_args /home/smarchi/src/babeltrace/src/cli/babeltrace2-cfg-cli-args.c:3445
    #3 0x558c39c1f9e4 in bt_config_cli_args_create /home/smarchi/src/babeltrace/src/cli/babeltrace2-cfg-cli-args.c:4654
    #4 0x558c39c233db in bt_config_cli_args_create_with_default /home/smarchi/src/babeltrace/src/cli/babeltrace2-cfg-cli-args-default.c:74
    #5 0x558c39c0781a in main /home/smarchi/src/babeltrace/src/cli/babeltrace2.c:2647
    #6 0x7f1e35757b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)

Reported-by: ASan
Change-Id: Ib2251d9dd8628bda128ae4d2e756d0d86fa12163
Signed-off-by: Simon Marchi <simon.marchi@efficios.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/2737
Tested-by: jenkins <jenkins@lttng.org>
src/cli/babeltrace2-cfg-cli-args.c

index e6fce2f7a2f92901632d5586db1839804a4b4f31..420e1efc0926ec18a98acfcd08c44caaf2c46ef0 100644 (file)
@@ -3440,6 +3440,7 @@ struct bt_config *bt_config_convert_from_args(int argc, const char *argv[],
                                        }
                                } else if (current_item_type == CONVERT_CURRENT_ITEM_TYPE_NON_OPT) {
                                        uint64_t idx = bt_value_array_get_length(non_opt_loglevels) - 1;
+                                       enum bt_value_array_set_element_by_index_status set_element_status;
                                        bt_value *log_level_str_value;
 
                                        log_level_str_value = bt_value_string_create_init(arg);
@@ -3448,9 +3449,11 @@ struct bt_config *bt_config_convert_from_args(int argc, const char *argv[],
                                                goto error;
                                        }
 
-                                       if (bt_value_array_set_element_by_index(non_opt_loglevels, idx,
-                                                       log_level_str_value)) {
-                                               bt_value_put_ref(log_level_str_value);
+                                       set_element_status =
+                                               bt_value_array_set_element_by_index(non_opt_loglevels,
+                                                       idx, log_level_str_value);
+                                       bt_value_put_ref(log_level_str_value);
+                                       if (set_element_status != BT_VALUE_ARRAY_SET_ELEMENT_BY_INDEX_STATUS_OK) {
                                                BT_CLI_LOGE_APPEND_CAUSE_OOM();
                                                goto error;
                                        }
This page took 0.026545 seconds and 4 git commands to generate.