Background
==========
Commit
601b0d3 changed the return value of the
`bt_value_array_get_size()` function now named
`bt_value_array_get_length()` from int64_t to uint64_t.
Problems
========
1. Multiple places in the code use the wrong storage type to store the
result of this function. Not only did we use `int64_t` but also, `int`,
`unsigned int` and `size_t` at some places.
2. A few places check for negative return value for handling potential
errors of this function. Because this function cannot fail and cannot
return a negative number, this is useless.
3. The babeltrace2.c file (`set_stream_intersections()` function)
contains two instances of checks against a negative value that would log
an error about the array being empty. Those log statements are
unreachable because the type is unsigned.
Solution
========
1. This commit cleanups so that we use `uint64_t` to store these value
and that we are using `uint64_t` for indexes looping on such value.
2. Remove useless error checks.
3. Those log statements are useful, so this commit corrects the
condition to compare against 0. But, this means that there is a change
in behaviour here. For example, before this commit, when doing the
stream intersection on an empty `bt_value_array` of traces the function would
return success and not log any error; now it fails and log an error.
Signed-off-by: Francis Deslauriers <francis.deslauriers@efficios.com>
Change-Id: I611bae82b4cb7092f13373283f358d8f7d13ae9d
Reviewed-on: https://review.lttng.org/c/babeltrace/+/2249
Tested-by: jenkins <jenkins@lttng.org>
char *error_buf, size_t error_buf_size)
{
int ret;
- size_t i;
+ uint64_t i;
if (!all_named_and_printable(cfg)) {
snprintf(error_buf, error_buf_size,
const bt_value *names_array, const char *prefix)
{
int ret = 0;
- int i;
+ uint64_t i;
GString *tmpstr = NULL, *default_value = NULL;
bool default_set = false, non_default_set = false;
{
struct bt_config *cfg = NULL;
const char **argv;
- int64_t i, len;
- const size_t argc = bt_value_array_get_length(run_args);
+ uint64_t i, len = bt_value_array_get_length(run_args);
- argv = calloc(argc, sizeof(*argv));
+ BT_ASSERT(len <= SIZE_MAX);
+ argv = calloc((size_t) len, sizeof(*argv));
if (!argv) {
BT_CLI_LOGE_APPEND_CAUSE_OOM();
goto end;
}
- len = bt_value_array_get_length(run_args);
- if (len < 0) {
- BT_CLI_LOGE_APPEND_CAUSE("Invalid executable arguments.");
- goto end;
- }
for (i = 0; i < len; i++) {
const bt_value *arg_value =
bt_value_array_borrow_element_by_index_const(run_args,
argv[i] = arg;
}
- cfg = bt_config_run_from_args(argc, argv, retcode,
+ cfg = bt_config_run_from_args((int) len, argv, retcode,
plugin_paths, default_log_level);
end:
bt_value *run_args)
{
int ret = 0;
- size_t i;
+ uint64_t i;
GString *component_arg_for_run = NULL;
if (!impl_args->exists) {
}
}
- for (i = 0; i < bt_value_array_get_length(impl_args->extra_params);
- i++) {
+ for (i = 0; i < bt_value_array_get_length(impl_args->extra_params); i++) {
const bt_value *elem;
const char *arg;
* here.
*/
if (print_run_args || print_run_args_0) {
+ uint64_t args_idx, args_len;
if (stream_intersection_mode) {
BT_CLI_LOGE_APPEND_CAUSE("Cannot specify --stream-intersection with --run-args or --run-args-0.");
goto error;
}
- for (i = 0; i < bt_value_array_get_length(run_args); i++) {
+ args_len = bt_value_array_get_length(run_args);
+ for (args_idx = 0; args_idx < args_len; args_idx++) {
const bt_value *arg_value =
bt_value_array_borrow_element_by_index(run_args,
- i);
+ args_idx);
const char *arg;
GString *quoted = NULL;
const char *arg_to_print;
g_string_free(quoted, TRUE);
}
- if (i < bt_value_array_get_length(run_args) - 1) {
+ if (args_idx < args_len - 1) {
if (print_run_args) {
putchar(' ');
} else {
int nr_paths, i, ret = 0;
nr_paths = bt_value_array_get_length(plugin_paths);
- if (nr_paths < 0) {
- BT_CLI_LOGE_APPEND_CAUSE(
- "Cannot load dynamic plugins: no plugin path.");
- ret = -1;
+ if (nr_paths == 0) {
+ BT_LOGI_STR("No dynamic plugin path.");
goto end;
}
uint64_t uint_val;
double dbl_val;
const char *str_val;
- int size;
GPtrArray *map_keys = NULL;
if (!value) {
break;
case BT_VALUE_TYPE_ARRAY:
{
- guint i;
+ uint64_t i, size;
size = bt_value_array_get_length(value);
- if (size < 0) {
- goto error;
- }
-
if (size == 0) {
print_indent(fp, indent);
fprintf(fp, "[ ]\n");
static const char * const comp_cls_name = "lttng-live";
static const bt_component_class_type comp_cls_type =
BT_COMPONENT_CLASS_TYPE_SOURCE;
- int64_t array_size, i;
+ uint64_t array_size, i;
const char *fail_reason = NULL;
FILE *out_stream = stdout;
int compute_stream_intersection(const bt_value *streams,
struct trace_range *range)
{
- unsigned int i;
- unsigned int stream_count;
+ uint64_t i, stream_count;
int ret;
BT_ASSERT(bt_value_get_type(streams) == BT_VALUE_TYPE_ARRAY);
{
int ret = 0;
uint64_t trace_idx;
- int64_t trace_count;
+ uint64_t trace_count;
const bt_value *query_result = NULL;
const bt_value *trace_info = NULL;
const bt_value *stream_infos = NULL;
}
trace_count = bt_value_array_get_length(query_result);
- if (trace_count < 0) {
+ if (trace_count == 0) {
BT_CLI_LOGE_APPEND_CAUSE("`babeltrace.trace-infos` query: result is empty: "
"component-class-name=%s", bt_component_class_get_name(comp_cls));
ret = -1;
}
stream_count = bt_value_array_get_length(stream_infos);
- if (stream_count < 0) {
+ if (stream_count == 0) {
ret = -1;
BT_CLI_LOGE_APPEND_CAUSE("`babeltrace.trace-infos` query: list of streams is empty: "
"component-class-name=%s",
}
case BT_VALUE_TYPE_ARRAY:
{
- int64_t count = bt_value_array_get_length(value);
+ uint64_t count = bt_value_array_get_length(value);
- BT_ASSERT(count >= 0);
BUF_APPEND(", %selement-count=%" PRId64, PRFIELD(count));
break;
}
struct bt_value *bt_attributes_borrow_field_by_name(
struct bt_value *attr_obj, const char *name)
{
- uint64_t i;
- int64_t attr_size;
+ uint64_t i, attr_size;
struct bt_value *value_obj = NULL;
struct bt_value *attr_field_name_obj = NULL;
attr_size = bt_value_array_get_length(attr_obj);
- if (attr_size < 0) {
- BT_LIB_LOGE_APPEND_CAUSE(
- "Cannot get array value's size: %![value-]+v",
- attr_obj);
- goto error;
- }
-
for (i = 0; i < attr_size; ++i) {
const char *field_name;
BT_HIDDEN
int bt_attributes_freeze(const struct bt_value *attr_obj)
{
- uint64_t i;
- int64_t count;
+ uint64_t i, count;
int ret = 0;
BT_ASSERT(attr_obj);
BT_LOGD("Freezing attributes object: value-addr=%p", attr_obj);
+
count = bt_value_array_get_length(attr_obj);
- BT_ASSERT(count >= 0);
/*
* We do not freeze the array value object itself here, since
const struct lttng_viewer_session *session,
bool *_found, struct live_viewer_connection *viewer_connection)
{
- int i, len, ret = 0;
+ int ret = 0;
+ uint64_t i, len;
bt_value *map = NULL;
bt_value *hostname = NULL;
bt_value *session_name = NULL;
bool found = false;
len = bt_value_array_get_length(results);
- if (len < 0) {
- BT_COMP_LOGE_STR("Error getting size of array.");
- ret = -1;
- goto end;
- }
for (i = 0; i < len; i++) {
const char *hostname_str = NULL;
const char *session_name_str = NULL;
- map = bt_value_array_borrow_element_by_index(results, (size_t) i);
+ map = bt_value_array_borrow_element_by_index(results, i);
if (!map) {
BT_COMP_LOGE_STR("Error borrowing map.");
ret = -1;