Enumeration mapping iterator's initial position is inconsistent
[babeltrace.git] / lib / ctf-ir / field-types.c
index 68ae5899a5dffd9baa527409bc94c081346cdd58..5063ea65b87f7f8a842610d421c3923ecafb5276 100644 (file)
@@ -697,25 +697,6 @@ bt_bool bt_ctf_field_type_enumeration_has_overlapping_ranges(
        return enumeration_type->has_overlapping_ranges;
 }
 
-static
-int bt_ctf_field_type_enumeration_get_mapping_name(
-               struct bt_ctf_field_type *enum_field_type,
-               uint64_t index,
-               const char **mapping_name)
-{
-       int ret = 0;
-       struct enumeration_mapping *mapping;
-
-       assert(enum_field_type);
-       mapping = get_enumeration_mapping(enum_field_type, index);
-       assert(mapping);
-       if (mapping_name) {
-               *mapping_name = g_quark_to_string(mapping->string);
-       }
-
-       return ret;
-}
-
 static
 int bt_ctf_field_type_variant_validate(struct bt_ctf_field_type *type)
 {
@@ -726,7 +707,6 @@ int bt_ctf_field_type_variant_validate(struct bt_ctf_field_type *type)
                container_of(type, struct bt_ctf_field_type_variant,
                        parent);
        int64_t i;
-       int64_t tag_mappings_count;
 
        if (variant->tag_name->len == 0) {
                BT_LOGW("Invalid variant field type: no tag field name: "
@@ -753,44 +733,20 @@ int bt_ctf_field_type_variant_validate(struct bt_ctf_field_type *type)
                goto end;
        }
 
-       tag_mappings_count =
-               bt_ctf_field_type_enumeration_get_mapping_count(
-                       (struct bt_ctf_field_type *) variant->tag);
-       assert(tag_mappings_count >= 0);
-
        /*
-        * Validate that each mapping found in the tag has a name which
-        * is also the name of a field in this variant field type.
+        * It is valid to have a variant field type which does not have
+        * the fields corresponding to each label in the associated
+        * enumeration.
         *
-        * The opposite is accepted: variant FT fields which cannot be
-        * selected because the variant FT tag has no mapping named as
-        * such. This scenario, while not ideal, cannot cause any error.
+        * It is also valid to have variant field type fields which
+        * cannot be selected because the variant field type tag has no
+        * mapping named as such. This scenario, while not ideal, cannot
+        * cause any error.
+        *
+        * If a non-existing field happens to be selected by an
+        * enumeration while reading a variant field, an error will be
+        * generated at that point (while reading the stream).
         */
-       for (i = 0; i < tag_mappings_count; ++i) {
-               const char *label;
-               struct bt_ctf_field_type *ft;
-
-               ret = bt_ctf_field_type_enumeration_get_mapping_name(
-                       (struct bt_ctf_field_type *) variant->tag,
-                       i, &label);
-               assert(ret == 0);
-               assert(label);
-               ft = bt_ctf_field_type_variant_get_field_type_by_name(
-                       type, label);
-               if (!ft) {
-                       BT_LOGW("Invalid variant field type: "
-                               "enumeration tag field type contains a mapping which does not name a variant field type field: "
-                               "variant-ft-addr=%p, tag-field-name=\"%s\", "
-                               "enum-ft-addr=%p, mapping-name=\"%s\"",
-                               type, variant->tag_name->str, variant->tag,
-                               label);
-                       ret = -1;
-                       goto end;
-               }
-
-               BT_PUT(ft);
-       }
-
        field_count = bt_ctf_field_type_variant_get_field_count(type);
        if (field_count < 0) {
                BT_LOGW("Invalid variant field type: no fields: "
@@ -1073,6 +1029,7 @@ int bt_ctf_field_type_integer_set_base(struct bt_ctf_field_type *type,
        }
 
        switch (base) {
+       case BT_CTF_INTEGER_BASE_UNSPECIFIED:
        case BT_CTF_INTEGER_BASE_BINARY:
        case BT_CTF_INTEGER_BASE_OCTAL:
        case BT_CTF_INTEGER_BASE_DECIMAL:
@@ -1322,21 +1279,12 @@ bt_ctf_field_type_enumeration_find_mappings_by_name(
 
        iter->u.name_quark = g_quark_try_string(name);
        if (!iter->u.name_quark) {
-               BT_LOGV("No such enumeration field type mapping name: "
-                       "ft-addr=%p, mapping-name=\"%s\"",
-                       type, name);
-               goto error;
-       }
-
-       /* Advance iterator to first entry, or leave index at -1. */
-       if (bt_ctf_field_type_enumeration_mapping_iterator_next(iter)) {
-               /* No entry found. */
-               BT_LOGV("No such enumeration field type mapping name: "
-                       "ft-addr=%p, mapping-name=\"%s\"",
-                       type, name);
-               goto error;
+               /*
+                * No results are possible, set the iterator's position at the
+                * end.
+                */
+               iter->index = iter->enumeration_type->entries->len;
        }
-
        return iter;
 error:
        bt_put(iter);
@@ -1427,13 +1375,6 @@ bt_ctf_field_type_enumeration_find_mappings_by_signed_value(
        }
 
        iter->u.signed_value = value;
-
-       /* Advance iterator to first entry, or leave index at -1. */
-       if (bt_ctf_field_type_enumeration_mapping_iterator_next(iter)) {
-               /* No entry found. */
-               goto error;
-       }
-
        return iter;
 error:
        bt_put(iter);
@@ -1444,12 +1385,7 @@ struct bt_ctf_field_type_enumeration_mapping_iterator *
 bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value(
                struct bt_ctf_field_type *type, uint64_t value)
 {
-       struct bt_ctf_field_type_enumeration_mapping_iterator *iter = NULL;
-
-       if (!type) {
-               BT_LOGW_STR("Invalid parameter: field type is NULL.");
-               goto error;
-       }
+       struct bt_ctf_field_type_enumeration_mapping_iterator *iter;
 
        iter = bt_ctf_field_type_enumeration_find_mappings_type(
                        type, ITERATOR_BY_UNSIGNED_VALUE);
@@ -1467,13 +1403,6 @@ bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value(
                goto error;
        }
        iter->u.unsigned_value = value;
-
-       /* Advance iterator to first entry, or leave index at -1. */
-       if (bt_ctf_field_type_enumeration_mapping_iterator_next(iter)) {
-               /* No entry found. */
-               goto error;
-       }
-
        return iter;
 error:
        bt_put(iter);
@@ -1493,6 +1422,12 @@ int bt_ctf_field_type_enumeration_mapping_iterator_get_signed(
                goto end;
        }
 
+       if (iter->index == -1) {
+               BT_LOGW_STR("Invalid enumeration field type mapping iterator access: position=-1");
+               ret = -1;
+               goto end;
+       }
+
        ret = bt_ctf_field_type_enumeration_get_mapping_signed(
                        &iter->enumeration_type->parent, iter->index,
                        mapping_name, range_begin, range_end);
@@ -1513,6 +1448,12 @@ int bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned(
                goto end;
        }
 
+       if (iter->index == -1) {
+               BT_LOGW_STR("Invalid enumeration field type mapping iterator access: position=-1");
+               ret = -1;
+               goto end;
+       }
+
        ret = bt_ctf_field_type_enumeration_get_mapping_unsigned(
                        &iter->enumeration_type->parent, iter->index,
                        mapping_name, range_begin, range_end);
@@ -2568,7 +2509,8 @@ struct bt_ctf_field_type *bt_ctf_field_type_variant_get_field_type_from_tag(
        }
 
        iter = bt_ctf_field_enumeration_get_mappings(tag);
-       if (!iter) {
+       ret = bt_ctf_field_type_enumeration_mapping_iterator_next(iter);
+       if (!iter || ret) {
                BT_LOGE("Cannot get enumeration field type mapping iterator from enumeration field: "
                        "enum-field-addr=%p", tag);
                goto end;
@@ -3900,6 +3842,7 @@ const char *get_integer_base_string(enum bt_ctf_integer_base base)
 
        switch (base) {
        case BT_CTF_INTEGER_BASE_DECIMAL:
+       case BT_CTF_INTEGER_BASE_UNSPECIFIED:
                base_string = "decimal";
                break;
        case BT_CTF_INTEGER_BASE_HEXADECIMAL:
@@ -4065,7 +4008,7 @@ int bt_ctf_field_type_structure_serialize(struct bt_ctf_field_type *type,
                struct structure_field *field = structure->fields->pdata[i];
 
                BT_LOGD("Serializing structure field type's field metadata: "
-                       "index=%" PRId64 ", "
+                       "index=%zu, "
                        "field-ft-addr=%p, field-name=\"%s\"",
                        i, field, g_quark_to_string(field->name));
 
@@ -4079,7 +4022,7 @@ int bt_ctf_field_type_structure_serialize(struct bt_ctf_field_type *type,
                ret = bt_ctf_field_type_serialize(field->type, context);
                if (ret) {
                        BT_LOGW("Cannot serialize structure field type's field's metadata: "
-                               "index=%" PRId64 ", "
+                               "index=%zu, "
                                "field-ft-addr=%p, field-name=\"%s\"",
                                i, field->type,
                                g_quark_to_string(field->name));
@@ -4133,7 +4076,7 @@ int bt_ctf_field_type_variant_serialize(struct bt_ctf_field_type *type,
                struct structure_field *field = variant->fields->pdata[i];
 
                BT_LOGD("Serializing variant field type's field metadata: "
-                       "index=%" PRId64 ", "
+                       "index=%zu, "
                        "field-ft-addr=%p, field-name=\"%s\"",
                        i, field, g_quark_to_string(field->name));
 
@@ -4149,7 +4092,7 @@ int bt_ctf_field_type_variant_serialize(struct bt_ctf_field_type *type,
                ret = bt_ctf_field_type_serialize(field->type, context);
                if (ret) {
                        BT_LOGW("Cannot serialize variant field type's field's metadata: "
-                               "index=%" PRId64 ", "
+                               "index=%zu, "
                                "field-ft-addr=%p, field-name=\"%s\"",
                                i, field->type,
                                g_quark_to_string(field->name));
@@ -5282,9 +5225,15 @@ struct bt_ctf_field_type *bt_ctf_field_type_get_field_at_index(
 
        switch (type_id) {
        case CTF_TYPE_STRUCT:
-               bt_ctf_field_type_structure_get_field_by_index(field_type,
-                       NULL, &field, index);
+       {
+               int ret = bt_ctf_field_type_structure_get_field_by_index(
+                       field_type, NULL, &field, index);
+               if (ret) {
+                       field = NULL;
+                       goto end;
+               }
                break;
+       }
        case CTF_TYPE_VARIANT:
        {
                int ret = bt_ctf_field_type_variant_get_field_by_index(
This page took 0.026252 seconds and 4 git commands to generate.