Cleanup: usages of bt_value_array_borrow_element_by_index{,_const}()
authorFrancis Deslauriers <francis.deslauriers@efficios.com>
Thu, 24 Oct 2019 16:17:49 +0000 (12:17 -0400)
committerFrancis Deslauriers <francis.deslauriers@efficios.com>
Sat, 26 Oct 2019 13:42:52 +0000 (09:42 -0400)
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 <francis.deslauriers@efficios.com>
Change-Id: Ifea5772d6b9f61487ef7295763f8f8929649b125
Reviewed-on: https://review.lttng.org/c/babeltrace/+/2253
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
Tested-by: jenkins <jenkins@lttng.org>
src/cli/babeltrace2-cfg-cli-args.c
src/cli/babeltrace2.c
src/lib/trace-ir/attributes.c
src/lib/value.c
src/plugins/ctf/lttng-live/viewer-connection.c
tests/lib/plugin.c
tests/lib/test_bt_values.c

index e8465b224749b8753a3cd525f9334952dc281a49..50c8ef006d6792d702d6c591ab801690ccd566fe 100644 (file)
@@ -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) {
index 6e867734c2067fb7b2f577cab895e4ac3d8a7e24..eaffebcdb5353e2cafd6cb7036ae789b05357d2a 100644 (file)
@@ -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: "
index 4512880a5c6ec903f26e3f5af669a677a85dc653..986ee3836531f4a1614ca2e9c49dbd628f32b3fc 100644 (file)
@@ -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;
index 9cef6ef2cc9d903915eae3be5e78307aa75a2e17..96c01ac453f9d79abcd0410cb82488a029f467e0 100644 (file)
@@ -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);
index 9b9a78e255e67ee4b2894f40db938c9c24f8c8a1..e8af3771b83c767929dfef58957551184a32ed85 100644 (file)
@@ -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.");
index 590cc29465031251c5588954154b938c49b3afd0..6ef8d5db44c8871693bc107dbf9967b27e6d9bc0 100644 (file)
@@ -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");
index 1dbb9b8d8c60b93cf977c90199522ee580ee57d0..4f9423eaf12eb834633f784b29048eeaa5531f2f 100644 (file)
@@ -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");
This page took 0.032634 seconds and 4 git commands to generate.