From: Simon Marchi Date: Tue, 7 Jan 2020 19:23:47 +0000 (-0500) Subject: cli: free log level string value X-Git-Tag: v2.0.0~24 X-Git-Url: http://git.efficios.com/?p=babeltrace.git;a=commitdiff_plain;h=d36a0b92f1ee42bcfd10e6600d27084f312f6bb9 cli: free log level string value 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 Reviewed-on: https://review.lttng.org/c/babeltrace/+/2737 Tested-by: jenkins --- diff --git a/src/cli/babeltrace2-cfg-cli-args.c b/src/cli/babeltrace2-cfg-cli-args.c index e6fce2f7..420e1efc 100644 --- a/src/cli/babeltrace2-cfg-cli-args.c +++ b/src/cli/babeltrace2-cfg-cli-args.c @@ -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; }