Fix: bt_ctf_trace_freeze must fail if type resolving fails
[babeltrace.git] / formats / ctf / ir / trace.c
index e7dcfdaede408f32953414c5dfbeae9fdd3633e4..32de5bb5f0d74929faffe4c68b05e0cabe84ec40 100644 (file)
@@ -32,6 +32,8 @@
 #include <babeltrace/ctf-ir/stream-class-internal.h>
 #include <babeltrace/ctf-writer/functor-internal.h>
 #include <babeltrace/ctf-ir/event-types-internal.h>
+#include <babeltrace/ctf-ir/attributes-internal.h>
+#include <babeltrace/ctf-ir/visitor-internal.h>
 #include <babeltrace/ctf-ir/utils.h>
 #include <babeltrace/compiler.h>
 #include <babeltrace/objects.h>
@@ -43,6 +45,8 @@ static
 void bt_ctf_trace_destroy(struct bt_ctf_ref *ref);
 static
 int init_trace_packet_header(struct bt_ctf_trace *trace);
+static
+int bt_ctf_trace_freeze(struct bt_ctf_trace *trace);
 
 static
 const unsigned int field_type_aliases_alignments[] = {
@@ -62,6 +66,13 @@ const unsigned int field_type_aliases_sizes[] = {
        [FIELD_TYPE_ALIAS_UINT64_T] = 64,
 };
 
+static
+void put_stream_class(struct bt_ctf_stream_class *stream_class)
+{
+       (void) bt_ctf_stream_class_set_trace(stream_class, NULL);
+       bt_ctf_stream_class_put(stream_class);
+}
+
 struct bt_ctf_trace *bt_ctf_trace_create(void)
 {
        struct bt_ctf_trace *trace = NULL;
@@ -74,11 +85,11 @@ struct bt_ctf_trace *bt_ctf_trace_create(void)
        bt_ctf_trace_set_byte_order(trace, BT_CTF_BYTE_ORDER_NATIVE);
        bt_ctf_ref_init(&trace->ref_count);
        trace->clocks = g_ptr_array_new_with_free_func(
-               (GDestroyNotify)bt_ctf_clock_put);
+               (GDestroyNotify) bt_ctf_clock_put);
        trace->streams = g_ptr_array_new_with_free_func(
-               (GDestroyNotify)bt_ctf_stream_put);
+               (GDestroyNotify) bt_ctf_stream_put);
        trace->stream_classes = g_ptr_array_new_with_free_func(
-               (GDestroyNotify)bt_ctf_stream_class_put);
+               (GDestroyNotify) put_stream_class);
        if (!trace->clocks || !trace->stream_classes || !trace->streams) {
                goto error_destroy;
        }
@@ -177,7 +188,7 @@ int bt_ctf_trace_set_environment_field(struct bt_ctf_trace *trace,
 {
        int ret = 0;
 
-       if (!trace || trace->frozen || !name || !value ||
+       if (!trace || !name || !value ||
                bt_ctf_validate_identifier(name) ||
                !(bt_object_is_integer(value) || bt_object_is_string(value))) {
                ret = -1;
@@ -189,6 +200,26 @@ int bt_ctf_trace_set_environment_field(struct bt_ctf_trace *trace,
                goto end;
        }
 
+       if (trace->frozen) {
+               /*
+                * New environment fields may be added to a frozen trace,
+                * but existing fields may not be changed.
+                *
+                * The object passed is frozen like all other attributes.
+                */
+               struct bt_object *attribute =
+                       bt_ctf_attributes_get_field_value_by_name(
+                               trace->environment, name);
+
+               if (attribute) {
+                       BT_OBJECT_PUT(attribute);
+                       ret = -1;
+                       goto end;
+               }
+
+               bt_object_freeze(value);
+       }
+
        ret = bt_ctf_attributes_set_field_value(trace->environment, name,
                value);
 
@@ -207,6 +238,22 @@ int bt_ctf_trace_set_environment_field_string(struct bt_ctf_trace *trace,
                goto end;
        }
 
+       if (trace->frozen) {
+               /*
+                * New environment fields may be added to a frozen trace,
+                * but existing fields may not be changed.
+                */
+               struct bt_object *attribute =
+                       bt_ctf_attributes_get_field_value_by_name(
+                               trace->environment, name);
+
+               if (attribute) {
+                       BT_OBJECT_PUT(attribute);
+                       ret = -1;
+                       goto end;
+               }
+       }
+
        env_value_string_obj = bt_object_string_create_init(value);
 
        if (!env_value_string_obj) {
@@ -214,6 +261,9 @@ int bt_ctf_trace_set_environment_field_string(struct bt_ctf_trace *trace,
                goto end;
        }
 
+       if (trace->frozen) {
+               bt_object_freeze(env_value_string_obj);
+       }
        ret = bt_ctf_trace_set_environment_field(trace, name,
                env_value_string_obj);
 
@@ -234,6 +284,22 @@ int bt_ctf_trace_set_environment_field_integer(struct bt_ctf_trace *trace,
                goto end;
        }
 
+       if (trace->frozen) {
+               /*
+                * New environment fields may be added to a frozen trace,
+                * but existing fields may not be changed.
+                */
+               struct bt_object *attribute =
+                       bt_ctf_attributes_get_field_value_by_name(
+                               trace->environment, name);
+
+               if (attribute) {
+                       BT_OBJECT_PUT(attribute);
+                       ret = -1;
+                       goto end;
+               }
+       }
+
        env_value_integer_obj = bt_object_integer_create_init(value);
        if (!env_value_integer_obj) {
                ret = -1;
@@ -242,7 +308,9 @@ int bt_ctf_trace_set_environment_field_integer(struct bt_ctf_trace *trace,
 
        ret = bt_ctf_trace_set_environment_field(trace, name,
                env_value_integer_obj);
-
+       if (trace->frozen) {
+               bt_object_freeze(env_value_integer_obj);
+       }
 end:
        BT_OBJECT_PUT(env_value_integer_obj);
 
@@ -376,11 +444,17 @@ int bt_ctf_trace_add_stream_class(struct bt_ctf_trace *trace,
 
        for (i = 0; i < trace->stream_classes->len; i++) {
                if (trace->stream_classes->pdata[i] == stream_class) {
+                       /* Stream already registered to the trace */
                        ret = -1;
                        goto end;
                }
        }
 
+       ret = bt_ctf_stream_class_resolve_types(stream_class, trace);
+       if (ret) {
+               goto end;
+       }
+
        stream_id = bt_ctf_stream_class_get_id(stream_class);
        if (stream_id < 0) {
                stream_id = trace->next_stream_id++;
@@ -395,7 +469,7 @@ int bt_ctf_trace_add_stream_class(struct bt_ctf_trace *trace,
                        }
                }
 
-               if (_bt_ctf_stream_class_set_id(stream_class,
+               if (bt_ctf_stream_class_set_id_no_check(stream_class,
                        stream_id)) {
                        /* TODO Should retry with a different stream id */
                        ret = -1;
@@ -403,6 +477,13 @@ int bt_ctf_trace_add_stream_class(struct bt_ctf_trace *trace,
                }
        }
 
+       /* Set weak reference to trace in stream class */
+       ret = bt_ctf_stream_class_set_trace(stream_class, trace);
+       if (ret) {
+               /* Stream class already part of another trace */
+               goto end;
+       }
+
        bt_ctf_stream_class_get(stream_class);
        g_ptr_array_add(trace->stream_classes, stream_class);
 
@@ -420,11 +501,16 @@ int bt_ctf_trace_add_stream_class(struct bt_ctf_trace *trace,
        if (ret) {
                goto end;
        }
-       bt_ctf_stream_class_freeze(stream_class);
-       trace->frozen = 1;
-       bt_ctf_attributes_freeze(trace->environment);
 
+       bt_ctf_stream_class_freeze(stream_class);
+       if (!trace->frozen) {
+               ret = bt_ctf_trace_freeze(trace);
+               goto end;
+       }
 end:
+       if (ret) {
+               (void) bt_ctf_stream_class_set_trace(stream_class, NULL);
+       }
        return ret;
 }
 
@@ -806,6 +892,22 @@ struct bt_ctf_field_type *get_field_type(enum field_type_alias alias)
        return field_type;
 }
 
+static
+int bt_ctf_trace_freeze(struct bt_ctf_trace *trace)
+{
+       int ret = 0;
+
+       ret = bt_ctf_trace_resolve_types(trace);
+       if (ret) {
+               goto end;
+       }
+
+       bt_ctf_attributes_freeze(trace->environment);
+       trace->frozen = 1;
+end:
+       return ret;
+}
+
 static
 int init_trace_packet_header(struct bt_ctf_trace *trace)
 {
This page took 0.028432 seconds and 4 git commands to generate.