Fix: Missing NULL check in bt_ctf_field_sequence_destroy
[babeltrace.git] / formats / ctf / ir / event-fields.c
index e05fd3244bc53749614f3e7698cb423f121058e7..2ca21f10059b05bc1a459a79ef7334d8749a8afc 100644 (file)
@@ -652,6 +652,31 @@ end:
        return new_field;
 }
 
+struct bt_ctf_field *bt_ctf_field_variant_get_current_field(
+               struct bt_ctf_field *variant_field)
+{
+       struct bt_ctf_field *current_field = NULL;
+       struct bt_ctf_field_variant *variant;
+
+       if (!variant_field ||
+               bt_ctf_field_type_get_type_id(variant_field->type) !=
+                       CTF_TYPE_VARIANT) {
+               goto end;
+       }
+
+       variant = container_of(variant_field, struct bt_ctf_field_variant,
+               parent);
+
+       if (variant->payload) {
+               current_field = variant->payload;
+               bt_ctf_field_get(current_field);
+               goto end;
+       }
+
+end:
+       return current_field;
+}
+
 struct bt_ctf_field *bt_ctf_field_enumeration_get_container(
        struct bt_ctf_field *field)
 {
@@ -1081,7 +1106,6 @@ end:
        return ret;
 }
 
-BT_HIDDEN
 struct bt_ctf_field *bt_ctf_field_copy(struct bt_ctf_field *field)
 {
        int ret;
@@ -1370,7 +1394,9 @@ void bt_ctf_field_sequence_destroy(struct bt_ctf_field *field)
        }
 
        sequence = container_of(field, struct bt_ctf_field_sequence, parent);
-       g_ptr_array_free(sequence->elements, TRUE);
+       if (sequence->elements) {
+               g_ptr_array_free(sequence->elements, TRUE);
+       }
        bt_ctf_field_put(sequence->length);
        g_free(sequence);
 }
@@ -1932,13 +1958,19 @@ int bt_ctf_field_structure_copy(struct bt_ctf_field *src,
        g_ptr_array_set_size(struct_dst->fields, struct_src->fields->len);
 
        for (i = 0; i < struct_src->fields->len; i++) {
-               struct bt_ctf_field *field_copy = bt_ctf_field_copy(
-                       g_ptr_array_index(struct_src->fields, i));
+               struct bt_ctf_field *field =
+                       g_ptr_array_index(struct_src->fields, i);
+               struct bt_ctf_field *field_copy = NULL;
 
-               if (!field_copy) {
-                       ret = -1;
-                       goto end;
+               if (field) {
+                       field_copy = bt_ctf_field_copy(field);
+
+                       if (!field_copy) {
+                               ret = -1;
+                               goto end;
+                       }
                }
+
                g_ptr_array_index(struct_dst->fields, i) = field_copy;
        }
 end:
@@ -1985,13 +2017,19 @@ int bt_ctf_field_array_copy(struct bt_ctf_field *src,
 
        g_ptr_array_set_size(array_dst->elements, array_src->elements->len);
        for (i = 0; i < array_src->elements->len; i++) {
-               struct bt_ctf_field *field_copy = bt_ctf_field_copy(
-                       g_ptr_array_index(array_src->elements, i));
+               struct bt_ctf_field *field =
+                       g_ptr_array_index(array_src->elements, i);
+               struct bt_ctf_field *field_copy = NULL;
 
-               if (!field_copy) {
-                       ret = -1;
-                       goto end;
+               if (field) {
+                       field_copy = bt_ctf_field_copy(field);
+
+                       if (!field_copy) {
+                               ret = -1;
+                               goto end;
+                       }
                }
+
                g_ptr_array_index(array_dst->elements, i) = field_copy;
        }
 end:
@@ -2037,12 +2075,17 @@ int bt_ctf_field_sequence_copy(struct bt_ctf_field *src,
        assert(sequence_dst->elements->len == sequence_src->elements->len);
 
        for (i = 0; i < sequence_src->elements->len; i++) {
-               struct bt_ctf_field *field_copy = bt_ctf_field_copy(
-                       g_ptr_array_index(sequence_src->elements, i));
+               struct bt_ctf_field *field =
+                       g_ptr_array_index(sequence_src->elements, i);
+               struct bt_ctf_field *field_copy = NULL;
 
-               if (!field_copy) {
-                       ret = -1;
-                       goto end;
+               if (field) {
+                       field_copy = bt_ctf_field_copy(field);
+
+                       if (!field_copy) {
+                               ret = -1;
+                               goto end;
+                       }
                }
 
                g_ptr_array_index(sequence_dst->elements, i) = field_copy;
This page took 0.02423 seconds and 4 git commands to generate.