Fix: avoid -Wmaybe-uninitialized warning in validate_map_value_entry
authorSimon Marchi <simon.marchi@efficios.com>
Fri, 11 Oct 2019 19:12:33 +0000 (15:12 -0400)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Thu, 17 Oct 2019 16:13:26 +0000 (12:13 -0400)
We get this warning when building with gcc at -Og:

    /home/smarchi/src/babeltrace/src/plugins/common/param-validation/param-validation.c: In function ‘validate_map_value_entry’:
    /home/smarchi/src/babeltrace/src/plugins/common/param-validation/param-validation.c:182:18: error: ‘candidate’ may be used uninitialized in this function [-Werror=maybe-uninitialized]
       data->status = validate_value(value, &candidate->value_descr,
                      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        data->ctx);
        ~~~~~~~~~~

Even though the code looks safe, we can modify it a little bit to avoid
that warning, and maybe even be a bit more readable.

Change-Id: I7a87d8125732744c4ff45fc0c0863c6dc9c26173
Signed-off-by: Simon Marchi <simon.marchi@efficios.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/2176
Reviewed-by: Francis Deslauriers <francis.deslauriers@efficios.com>
src/plugins/common/param-validation/param-validation.c

index bdca9f9a35a1cf5239146768553712b973f45943..ca80e888add31aa156ae4bbcec2c3730bb46d3d7 100644 (file)
@@ -159,19 +159,21 @@ bt_bool validate_map_value_entry(const char *key,
                const bt_value *value, void *v_data)
 {
        struct validate_map_value_data *data = v_data;
-       const struct bt_param_validation_map_value_entry_descr *candidate;
+       const struct bt_param_validation_map_value_entry_descr *entry = NULL;
        guint i;
 
        /* Check if this key is in the available keys. */
        for (i = 0; i < data->available_keys->len; i++) {
-               candidate = g_ptr_array_index(data->available_keys, i);
+               const struct bt_param_validation_map_value_entry_descr *candidate =
+                       g_ptr_array_index(data->available_keys, i);
 
                if (g_str_equal(key, candidate->key)) {
+                       entry = candidate;
                        break;
                }
        }
 
-       if (i < data->available_keys->len) {
+       if (entry) {
                /* Key was found in available keys. */
                g_ptr_array_remove_index_fast(data->available_keys, i);
 
@@ -179,7 +181,7 @@ bt_bool validate_map_value_entry(const char *key,
                validate_ctx_push_map_scope(data->ctx, key);
 
                /* Validate the value of the entry. */
-               data->status = validate_value(value, &candidate->value_descr,
+               data->status = validate_value(value, &entry->value_descr,
                        data->ctx);
 
                validate_ctx_pop_scope(data->ctx);
This page took 0.02466 seconds and 4 git commands to generate.