Fix: ir: seq copy: initialize internal array
[babeltrace.git] / formats / ctf / ir / event-fields.c
index 92c06da8b0ec8a7bf58ae3cd76d8e0aea38e2bef..55316f49d88e52960b23807127f109255ae4544d 100644 (file)
@@ -946,6 +946,45 @@ end:
        return ret;
 }
 
+int bt_ctf_field_string_append_len(struct bt_ctf_field *field,
+               const char *value, unsigned int length)
+{
+       int i;
+       int ret = 0;
+       unsigned int effective_length = length;
+       struct bt_ctf_field_string *string_field;
+
+       if (!field || !value ||
+               bt_ctf_field_type_get_type_id(field->type) !=
+                       CTF_TYPE_STRING) {
+               ret = -1;
+               goto end;
+       }
+
+       string_field = container_of(field, struct bt_ctf_field_string, parent);
+
+       /* make sure no null bytes are appended */
+       for (i = 0; i < length; ++i) {
+               if (value[i] == '\0') {
+                       effective_length = i;
+                       break;
+               }
+       }
+
+       if (string_field->payload) {
+               g_string_append_len(string_field->payload, value,
+                       effective_length);
+       } else {
+               string_field->payload = g_string_new_len(value,
+                       effective_length);
+       }
+
+       string_field->parent.payload_set = 1;
+
+end:
+       return ret;
+}
+
 BT_HIDDEN
 int bt_ctf_field_validate(struct bt_ctf_field *field)
 {
@@ -1034,6 +1073,7 @@ struct bt_ctf_field *bt_ctf_field_copy(struct bt_ctf_field *field)
                goto end;
        }
 
+       copy->payload_set = field->payload_set;
        ret = field_copy_funcs[type_id](field, copy);
        if (ret) {
                bt_ctf_field_put(copy);
@@ -1935,12 +1975,38 @@ int bt_ctf_field_sequence_copy(struct bt_ctf_field *src,
 {
        int ret = 0, i;
        struct bt_ctf_field_sequence *sequence_src, *sequence_dst;
+       struct bt_ctf_field *src_length;
+       struct bt_ctf_field *dst_length;
 
        sequence_src = container_of(src, struct bt_ctf_field_sequence, parent);
        sequence_dst = container_of(dst, struct bt_ctf_field_sequence, parent);
 
-       g_ptr_array_set_size(sequence_dst->elements,
-               sequence_src->elements->len);
+       src_length = bt_ctf_field_sequence_get_length(src);
+
+       if (!src_length) {
+               /* no length set yet: keep destination sequence empty */
+               goto end;
+       }
+
+       /* copy source length */
+       dst_length = bt_ctf_field_copy(src_length);
+       bt_ctf_field_put(src_length);
+
+       if (!dst_length) {
+               ret = -1;
+               goto end;
+       }
+
+       /* this will initialize the destination sequence's internal array */
+       ret = bt_ctf_field_sequence_set_length(dst, dst_length);
+       bt_ctf_field_put(dst_length);
+
+       if (ret) {
+               goto end;
+       }
+
+       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));
@@ -1949,6 +2015,7 @@ int bt_ctf_field_sequence_copy(struct bt_ctf_field *src,
                        ret = -1;
                        goto end;
                }
+
                g_ptr_array_index(sequence_dst->elements, i) = field_copy;
        }
 end:
This page took 0.023754 seconds and 4 git commands to generate.