ctf.fs: bt_ctf_notif_iter_create(): assert() that all medops exist
[babeltrace.git] / types / variant.c
index 3641fa05d16b69a22074170917f5f653eaa17d4e..f660e18bfc33605f4c3af1463f20fee828f17752 100644 (file)
@@ -46,6 +46,9 @@ int bt_variant_rw(struct bt_stream_pos *ppos, struct bt_definition *definition)
        struct bt_definition *field;
 
        field = bt_variant_get_current_field(variant_definition);
+       if (!field) {
+               return -EINVAL;
+       }
        return generic_rw(ppos, field);
 }
 
@@ -224,8 +227,8 @@ void bt_untagged_variant_declaration_add_field(struct declaration_untagged_varia
        field->declaration = field_declaration;
        /* Keep index in hash rather than pointer, because array can relocate */
        g_hash_table_insert(untagged_variant_declaration->fields_by_tag,
-                           (gpointer) (unsigned long) field->name,
-                           (gpointer) index);
+                           GUINT_TO_POINTER(field->name),
+                           GUINT_TO_POINTER(index));
        /*
         * Alignment of variant is based on the alignment of its currently
         * selected choice, so we leave variant alignment as-is (statically
@@ -241,13 +244,13 @@ bt_untagged_variant_declaration_get_field_from_tag(struct declaration_untagged_v
 
        found = g_hash_table_lookup_extended(
                                untagged_variant_declaration->fields_by_tag,
-                               (gconstpointer) (unsigned long) tag, NULL, &index);
+                               (gconstpointer) GUINT_TO_POINTER(tag), NULL, &index);
 
        if (!found) {
                return NULL;
        }
 
-       return &g_array_index(untagged_variant_declaration->fields, struct declaration_field, (unsigned long)index);
+       return &g_array_index(untagged_variant_declaration->fields, struct declaration_field, GPOINTER_TO_UINT(index));
 }
 
 /*
@@ -261,16 +264,30 @@ struct bt_definition *bt_variant_get_current_field(struct definition_variant *va
        unsigned long index;
        GArray *tag_array;
        GQuark tag;
+       gpointer orig_key, value;
 
        tag_array = _enum->value;
+       if (!tag_array) {
+               /* Enumeration has unknown tag. */
+               fprintf(stderr, "[error] Enumeration used for variant has unknown tag.\n");
+               return NULL;
+       }
        /*
         * The 1 to 1 mapping from enumeration to value should have been already
         * checked. (see TODO above)
         */
        assert(tag_array->len == 1);
        tag = g_array_index(tag_array, GQuark, 0);
-       index = (unsigned long) g_hash_table_lookup(variant_declaration->untagged_variant->fields_by_tag,
-                                                   (gconstpointer) (unsigned long) tag);
+       if (!g_hash_table_lookup_extended(variant_declaration->untagged_variant->fields_by_tag,
+                       (gconstpointer) GUINT_TO_POINTER(tag),
+                       &orig_key,
+                       &value)) {
+               /* Cannot find matching field. */
+               fprintf(stderr, "[error] Cannot find matching field for enum field \"%s\" in variant.\n",
+                       g_quark_to_string(tag));
+               return NULL;
+       }
+       index = GPOINTER_TO_UINT(value);
        variant->current_field = g_ptr_array_index(variant->fields, index);
        return variant->current_field;
 }
This page took 0.024741 seconds and 4 git commands to generate.