From: Francis Deslauriers Date: Thu, 24 Oct 2019 16:17:49 +0000 (-0400) Subject: Cleanup: usages of bt_value_array_borrow_element_by_index{,_const}() X-Git-Tag: v2.0.0-rc2~46 X-Git-Url: http://git.efficios.com/?p=babeltrace.git;a=commitdiff_plain;h=85e6a1160e1dc8564a7bf86a9099f77f62cace7e Cleanup: usages of bt_value_array_borrow_element_by_index{,_const}() Checking the return value of this function against NULL is mostly useless because in case of an out-of-bound access the function would return a pointer value passed the end of the array (junk). This commit removes NULL checks and `BT_ASSERT()`on the return value of this function. Signed-off-by: Francis Deslauriers Change-Id: Ifea5772d6b9f61487ef7295763f8f8929649b125 Reviewed-on: https://review.lttng.org/c/babeltrace/+/2253 Reviewed-by: Simon Marchi Tested-by: jenkins --- diff --git a/src/cli/babeltrace2-cfg-cli-args.c b/src/cli/babeltrace2-cfg-cli-args.c index e8465b22..50c8ef00 100644 --- a/src/cli/babeltrace2-cfg-cli-args.c +++ b/src/cli/babeltrace2-cfg-cli-args.c @@ -768,12 +768,6 @@ int insert_flat_params_from_array(GString *params_arg, const char *suffix; bool is_default = false; - if (!str_obj) { - BT_CLI_LOGE_APPEND_CAUSE("Unexpected error."); - ret = -1; - goto end; - } - suffix = bt_value_string_get(str_obj); g_string_assign(tmpstr, prefix); @@ -2088,7 +2082,6 @@ struct bt_config *bt_config_run_from_args_array(const bt_value *run_args, i); const char *arg; - BT_ASSERT(arg_value); arg = bt_value_string_get(arg_value); BT_ASSERT(arg); argv[i] = arg; @@ -2399,11 +2392,8 @@ int append_run_args_for_implicit_component( const bt_value *elem; const char *arg; - elem = bt_value_array_borrow_element_by_index(impl_args->extra_params, - i); - if (!elem) { - goto error; - } + elem = bt_value_array_borrow_element_by_index( + impl_args->extra_params, i); BT_ASSERT(bt_value_is_string(elem)); arg = bt_value_string_get(elem); @@ -4347,7 +4337,6 @@ struct bt_config *bt_config_convert_from_args(int argc, const char *argv[], GString *quoted = NULL; const char *arg_to_print; - BT_ASSERT(arg_value); arg = bt_value_string_get(arg_value); if (print_run_args) { diff --git a/src/cli/babeltrace2.c b/src/cli/babeltrace2.c index 6e867734..eaffebcd 100644 --- a/src/cli/babeltrace2.c +++ b/src/cli/babeltrace2.c @@ -390,9 +390,6 @@ void print_value_rec(FILE *fp, const bt_value *value, size_t indent) bt_value_array_borrow_element_by_index_const( value, i); - if (!element) { - goto error; - } print_indent(fp, indent); fprintf(fp, "- "); @@ -466,10 +463,6 @@ void print_value_rec(FILE *fp, const bt_value *value, size_t indent) goto end; -error: - BT_LOGE("Error printing value of type %s.", - bt_common_value_type_string(bt_value_get_type(value))); - end: if (map_keys) { g_ptr_array_free(map_keys, TRUE); @@ -1003,10 +996,6 @@ int cmd_print_lttng_live_sessions(struct bt_config *cfg) int64_t timer_us, streams, clients; map = bt_value_array_borrow_element_by_index_const(results, i); - if (!map) { - BT_CLI_LOGE_APPEND_CAUSE("Unexpected empty array entry."); - goto error; - } if (!bt_value_is_map(map)) { BT_CLI_LOGE_APPEND_CAUSE("Unexpected entry type."); goto error; @@ -2127,7 +2116,6 @@ int set_stream_intersections(struct cmd_run_ctx *ctx, trace_info = bt_value_array_borrow_element_by_index_const( query_result, trace_idx); - BT_ASSERT(trace_info); if (!bt_value_is_map(trace_info)) { ret = -1; BT_CLI_LOGE_APPEND_CAUSE("`babeltrace.trace-infos` query: expecting element to be a map: " @@ -2201,7 +2189,6 @@ int set_stream_intersections(struct cmd_run_ctx *ctx, stream_info = bt_value_array_borrow_element_by_index_const( stream_infos, stream_idx); - BT_ASSERT(stream_info); if (!bt_value_is_map(stream_info)) { ret = -1; BT_CLI_LOGE_APPEND_CAUSE("`babeltrace.trace-infos` query: " diff --git a/src/lib/trace-ir/attributes.c b/src/lib/trace-ir/attributes.c index 4512880a..986ee383 100644 --- a/src/lib/trace-ir/attributes.c +++ b/src/lib/trace-ir/attributes.c @@ -86,7 +86,6 @@ BT_HIDDEN const char *bt_attributes_get_field_name(const struct bt_value *attr_obj, uint64_t index) { - const char *ret = NULL; const struct bt_value *attr_field_obj = NULL; const struct bt_value *attr_field_name_obj = NULL; @@ -94,59 +93,28 @@ const char *bt_attributes_get_field_name(const struct bt_value *attr_obj, BT_ASSERT_DBG(index < bt_value_array_get_length(attr_obj)); attr_field_obj = bt_value_array_borrow_element_by_index_const( attr_obj, index); - if (!attr_field_obj) { - BT_LIB_LOGE_APPEND_CAUSE( - "Cannot borrow attributes object's array value's element by index: " - "%![value-]+v, index=%" PRIu64, attr_obj, index); - goto end; - } attr_field_name_obj = bt_value_array_borrow_element_by_index_const(attr_field_obj, BT_ATTR_NAME_INDEX); - if (!attr_field_name_obj) { - BT_LIB_LOGE_APPEND_CAUSE( - "Cannot get attribute array value's element by index: " - "%![value-]+v, index=%" PRIu64, attr_field_obj, - (uint64_t) BT_ATTR_NAME_INDEX); - goto end; - } - - ret = bt_value_string_get(attr_field_name_obj); -end: - return ret; + return bt_value_string_get(attr_field_name_obj); } BT_HIDDEN struct bt_value *bt_attributes_borrow_field_value( struct bt_value *attr_obj, uint64_t index) { - struct bt_value *value_obj = NULL; struct bt_value *attr_field_obj = NULL; BT_ASSERT_DBG(attr_obj); BT_ASSERT_DBG(index < bt_value_array_get_length(attr_obj)); + attr_field_obj = bt_value_array_borrow_element_by_index(attr_obj, index); - if (!attr_field_obj) { - BT_LIB_LOGE_APPEND_CAUSE( - "Cannot get attributes object's array value's element by index: " - "%![value-]+v, index=%" PRIu64, attr_obj, index); - goto end; - } - value_obj = bt_value_array_borrow_element_by_index( - attr_field_obj, BT_ATTR_VALUE_INDEX); - if (!value_obj) { - BT_LIB_LOGE_APPEND_CAUSE( - "Cannot get attribute array value's element by index: " - "%![value-]+v, index=%" PRIu64, attr_field_obj, - (uint64_t) BT_ATTR_VALUE_INDEX); - } - -end: - return value_obj; + return bt_value_array_borrow_element_by_index( attr_field_obj, + BT_ATTR_VALUE_INDEX); } static @@ -163,23 +131,10 @@ struct bt_value *bt_attributes_borrow_field_by_name( value_obj = bt_value_array_borrow_element_by_index( attr_obj, i); - if (!value_obj) { - BT_LIB_LOGE_APPEND_CAUSE( - "Cannot get attributes object's array value's element by index: " - "%![value-]+v, index=%" PRIu64, attr_obj, i); - goto error; - } attr_field_name_obj = bt_value_array_borrow_element_by_index( value_obj, BT_ATTR_NAME_INDEX); - if (!attr_field_name_obj) { - BT_LIB_LOGE_APPEND_CAUSE( - "Cannot get attribute array value's element by index: " - "%![value-]+v, index=%" PRIu64, - value_obj, (int64_t) BT_ATTR_NAME_INDEX); - goto error; - } field_name = bt_value_string_get(attr_field_name_obj); @@ -191,10 +146,6 @@ struct bt_value *bt_attributes_borrow_field_by_name( } return value_obj; - -error: - value_obj = NULL; - return value_obj; } BT_HIDDEN @@ -266,12 +217,6 @@ struct bt_value *bt_attributes_borrow_field_value_by_name( value_obj = bt_value_array_borrow_element_by_index( attr_field_obj, BT_ATTR_VALUE_INDEX); - if (!value_obj) { - BT_LIB_LOGE_APPEND_CAUSE( - "Cannot get attribute array value's element by index: " - "%![value-]+v, index=%" PRIu64, attr_field_obj, - (uint64_t) BT_ATTR_VALUE_INDEX); - } end: return value_obj; diff --git a/src/lib/value.c b/src/lib/value.c index 9cef6ef2..96c01ac4 100644 --- a/src/lib/value.c +++ b/src/lib/value.c @@ -197,7 +197,6 @@ struct bt_value *bt_value_array_copy(const struct bt_value *array_obj) bt_value_array_borrow_element_by_index_const( array_obj, i); - BT_ASSERT(element_obj); BT_LOGD("Copying array value's element: element-addr=%p, " "index=%d", element_obj, i); ret = bt_value_copy(element_obj, &element_obj_copy); diff --git a/src/plugins/ctf/lttng-live/viewer-connection.c b/src/plugins/ctf/lttng-live/viewer-connection.c index 9b9a78e2..e8af3771 100644 --- a/src/plugins/ctf/lttng-live/viewer-connection.c +++ b/src/plugins/ctf/lttng-live/viewer-connection.c @@ -318,11 +318,6 @@ int list_update_session(bt_value *results, const char *session_name_str = NULL; map = bt_value_array_borrow_element_by_index(results, i); - if (!map) { - BT_COMP_LOGE_STR("Error borrowing map."); - ret = -1; - goto end; - } hostname = bt_value_map_borrow_entry_value(map, "target-hostname"); if (!hostname) { BT_COMP_LOGE_STR("Error borrowing \"target-hostname\" entry."); diff --git a/tests/lib/plugin.c b/tests/lib/plugin.c index 590cc294..6ef8d5db 100644 --- a/tests/lib/plugin.c +++ b/tests/lib/plugin.c @@ -188,7 +188,7 @@ static void test_sfs(const char *plugin_dir) ok(ret == 0 && results, "bt_query_executor_query() succeeds"); BT_ASSERT(bt_value_is_array(results) && bt_value_array_get_length(results) == 2); object = bt_value_array_borrow_element_by_index_const(results, 0); - BT_ASSERT(object && bt_value_is_string(object)); + BT_ASSERT(bt_value_is_string(object)); object_str = bt_value_string_get(object); ok(strcmp(object_str, "get-something") == 0, "bt_component_class_query() receives the expected object name"); diff --git a/tests/lib/test_bt_values.c b/tests/lib/test_bt_values.c index 1dbb9b8d..4f9423ea 100644 --- a/tests/lib/test_bt_values.c +++ b/tests/lib/test_bt_values.c @@ -235,31 +235,31 @@ void test_array(void) "appending an element to an array value object increment its size"); obj = bt_value_array_borrow_element_by_index(array_obj, 0); - ok(obj && bt_value_is_unsigned_integer(obj), + ok(bt_value_is_unsigned_integer(obj), "bt_value_array_borrow_element_by_index() returns an value object with the appropriate type (unsigned integer)"); int_value = bt_value_integer_unsigned_get(obj); ok(int_value == 345, "bt_value_array_borrow_element_by_index() returns an value object with the appropriate value (unsigned integer)"); obj = bt_value_array_borrow_element_by_index(array_obj, 1); - ok(obj && bt_value_is_signed_integer(obj), + ok(bt_value_is_signed_integer(obj), "bt_value_array_borrow_element_by_index() returns an value object with the appropriate type (signed integer)"); int_value = bt_value_integer_signed_get(obj); ok(int_value == -507, "bt_value_array_borrow_element_by_index() returns an value object with the appropriate value (signed integer)"); obj = bt_value_array_borrow_element_by_index(array_obj, 2); - ok(obj && bt_value_is_real(obj), + ok(bt_value_is_real(obj), "bt_value_array_borrow_element_by_index() returns an value object with the appropriate type (real number)"); real_value = bt_value_real_get(obj); ok(real_value == -17.45, "bt_value_array_borrow_element_by_index() returns an value object with the appropriate value (real number)"); obj = bt_value_array_borrow_element_by_index(array_obj, 3); - ok(obj && bt_value_is_bool(obj), + ok(bt_value_is_bool(obj), "bt_value_array_borrow_element_by_index() returns an value object with the appropriate type (boolean)"); bool_value = bt_value_bool_get(obj); ok(bool_value, "bt_value_array_borrow_element_by_index() returns an value object with the appropriate value (boolean)"); obj = bt_value_array_borrow_element_by_index(array_obj, 4); - ok(obj == bt_value_null, + ok(bt_value_null, "bt_value_array_borrow_element_by_index() returns an value object with the appropriate type (null)"); obj = bt_value_integer_signed_create_init(1001); @@ -268,7 +268,7 @@ void test_array(void) "bt_value_array_set_element_by_index() succeeds"); BT_VALUE_PUT_REF_AND_RESET(obj); obj = bt_value_array_borrow_element_by_index(array_obj, 2); - ok(obj && bt_value_is_signed_integer(obj), + ok(bt_value_is_signed_integer(obj), "bt_value_array_set_element_by_index() inserts an value object with the appropriate type"); int_value = bt_value_integer_signed_get(obj); BT_ASSERT(!ret); @@ -313,52 +313,52 @@ void test_array(void) "map value object is not empty"); obj = bt_value_array_borrow_element_by_index(array_obj, 5); - ok(obj && bt_value_is_bool(obj), + ok(bt_value_is_bool(obj), "bt_value_array_append_bool_element() appends a boolean value object"); bool_value = bt_value_bool_get(obj); ok(!bool_value, "bt_value_array_append_bool_element() appends the appropriate value"); obj = bt_value_array_borrow_element_by_index(array_obj, 6); - ok(obj && bt_value_is_unsigned_integer(obj), + ok(bt_value_is_unsigned_integer(obj), "bt_value_array_append_unsigned_integer_element() appends an unsigned integer value object"); int_value = bt_value_integer_unsigned_get(obj); ok(int_value == 98765, "bt_value_array_append_unsigned_integer_element() appends the appropriate value"); obj = bt_value_array_borrow_element_by_index(array_obj, 7); - ok(obj && bt_value_is_signed_integer(obj), + ok(bt_value_is_signed_integer(obj), "bt_value_array_append_signed_integer_element() appends a signed integer value object"); int_value = bt_value_integer_signed_get(obj); ok(int_value == -10101, "bt_value_array_append_signed_integer_element() appends the appropriate value"); obj = bt_value_array_borrow_element_by_index(array_obj, 8); - ok(obj && bt_value_is_real(obj), + ok(bt_value_is_real(obj), "bt_value_array_append_real_element() appends a real number value object"); real_value = bt_value_real_get(obj); ok(real_value == 2.49578, "bt_value_array_append_real_element() appends the appropriate value"); obj = bt_value_array_borrow_element_by_index(array_obj, 9); - ok(obj && bt_value_is_string(obj), + ok(bt_value_is_string(obj), "bt_value_array_append_string_element() appends a string value object"); string_value = bt_value_string_get(obj); ok(!ret && string_value && strcmp(string_value, "bt_value") == 0, "bt_value_array_append_string_element() appends the appropriate value"); obj = bt_value_array_borrow_element_by_index(array_obj, 10); - ok(obj && bt_value_is_array(obj), + ok(bt_value_is_array(obj), "bt_value_array_append_empty_array_element() appends an array value object"); ok(bt_value_array_is_empty(obj), "bt_value_array_append_empty_array_element() an empty array value object"); obj = bt_value_array_borrow_element_by_index(array_obj, 11); - ok(obj && bt_value_is_array(obj), + ok(bt_value_is_array(obj), "bt_value_array_append_empty_array_element() appends an array value object"); ok(bt_value_array_is_empty(obj), "bt_value_array_append_empty_array_element() an empty array value object"); obj = bt_value_array_borrow_element_by_index(array_obj, 12); - ok(obj && bt_value_is_map(obj), + ok(bt_value_is_map(obj), "bt_value_array_append_empty_map_element() appends a map value object"); ok(bt_value_map_is_empty(obj), "bt_value_array_append_empty_map_element() an empty map value object"); obj = bt_value_array_borrow_element_by_index(array_obj, 13); - ok(obj && bt_value_is_map(obj), + ok(bt_value_is_map(obj), "bt_value_array_append_empty_map_element() appends a map value object"); ok(bt_value_map_is_empty(obj), "bt_value_array_append_empty_map_element() an empty map value object");