Fix: complete error handling of babeltrace API
[babeltrace.git] / formats / ctf / events.c
index 5d92b0872e418ceb69f473e4de3c969f11b96461..a9f5c22ee170815f0e5450d2c82d78cc27627400 100644 (file)
@@ -42,8 +42,12 @@ const struct definition *bt_ctf_get_top_level_scope(const struct bt_ctf_event *c
                enum bt_ctf_scope scope)
 {
        struct definition *tmp = NULL;
                enum bt_ctf_scope scope)
 {
        struct definition *tmp = NULL;
-       struct ctf_event_definition *event = ctf_event->parent;
+       struct ctf_event_definition *event;
 
 
+       if (!ctf_event)
+               return NULL;
+
+       event = ctf_event->parent;
        switch (scope) {
        case BT_TRACE_PACKET_HEADER:
                if (!event->stream)
        switch (scope) {
        case BT_TRACE_PACKET_HEADER:
                if (!event->stream)
@@ -91,28 +95,28 @@ const struct definition *bt_ctf_get_field(const struct bt_ctf_event *ctf_event,
        struct definition *def;
        char *field_underscore;
 
        struct definition *def;
        char *field_underscore;
 
-       if (scope) {
-               def = lookup_definition(scope, field);
-               /*
-                * optionally a field can have an underscore prefix, try
-                * to lookup the field with this prefix if it failed
-                */
-               if (!def) {
-                       field_underscore = g_new(char, strlen(field) + 2);
-                       field_underscore[0] = '_';
-                       strcpy(&field_underscore[1], field);
-                       def = lookup_definition(scope, field_underscore);
-                       g_free(field_underscore);
-               }
-               if (bt_ctf_field_type(def) == CTF_TYPE_VARIANT) {
-                       struct definition_variant *variant_definition;
-                       variant_definition = container_of(def,
-                                       struct definition_variant, p);
-                       return variant_definition->current_field;
-               }
-               return def;
+       if (!ctf_event || !scope || !field)
+               return NULL;
+
+       def = lookup_definition(scope, field);
+       /*
+        * optionally a field can have an underscore prefix, try
+        * to lookup the field with this prefix if it failed
+        */
+       if (!def) {
+               field_underscore = g_new(char, strlen(field) + 2);
+               field_underscore[0] = '_';
+               strcpy(&field_underscore[1], field);
+               def = lookup_definition(scope, field_underscore);
+               g_free(field_underscore);
        }
        }
-       return NULL;
+       if (bt_ctf_field_type(def) == CTF_TYPE_VARIANT) {
+               struct definition_variant *variant_definition;
+               variant_definition = container_of(def,
+                               struct definition_variant, p);
+               return variant_definition->current_field;
+       }
+       return def;
 }
 
 const struct definition *bt_ctf_get_index(const struct bt_ctf_event *ctf_event,
 }
 
 const struct definition *bt_ctf_get_index(const struct bt_ctf_event *ctf_event,
@@ -121,6 +125,9 @@ const struct definition *bt_ctf_get_index(const struct bt_ctf_event *ctf_event,
 {
        struct definition *ret = NULL;
 
 {
        struct definition *ret = NULL;
 
+       if (!ctf_event || !field)
+               return NULL;
+
        if (bt_ctf_field_type(field) == CTF_TYPE_ARRAY) {
                struct definition_array *array_definition;
                array_definition = container_of(field,
        if (bt_ctf_field_type(field) == CTF_TYPE_ARRAY) {
                struct definition_array *array_definition;
                array_definition = container_of(field,
@@ -139,10 +146,12 @@ const char *bt_ctf_event_name(const struct bt_ctf_event *ctf_event)
 {
        struct ctf_event_declaration *event_class;
        struct ctf_stream_declaration *stream_class;
 {
        struct ctf_event_declaration *event_class;
        struct ctf_stream_declaration *stream_class;
-       struct ctf_event_definition *event = ctf_event->parent;
+       struct ctf_event_definition *event;
 
 
-       if (!event)
+       if (!ctf_event)
                return NULL;
                return NULL;
+
+       event = ctf_event->parent;
        stream_class = event->stream->stream_class;
        event_class = g_ptr_array_index(stream_class->events_by_id,
                        event->stream->event_id);
        stream_class = event->stream->stream_class;
        event_class = g_ptr_array_index(stream_class->events_by_id,
                        event->stream->event_id);
@@ -151,16 +160,18 @@ const char *bt_ctf_event_name(const struct bt_ctf_event *ctf_event)
 
 const char *bt_ctf_field_name(const struct definition *def)
 {
 
 const char *bt_ctf_field_name(const struct definition *def)
 {
-       if (def)
-               return rem_(g_quark_to_string(def->name));
-       return NULL;
+       if (!def)
+               return NULL;
+
+       return rem_(g_quark_to_string(def->name));
 }
 
 enum ctf_type_id bt_ctf_field_type(const struct definition *def)
 {
 }
 
 enum ctf_type_id bt_ctf_field_type(const struct definition *def)
 {
-       if (def)
-               return def->declaration->id;
-       return CTF_TYPE_UNKNOWN;
+       if (!def)
+               return CTF_TYPE_UNKNOWN;
+
+       return def->declaration->id;
 }
 
 int bt_ctf_get_field_list(const struct bt_ctf_event *ctf_event,
 }
 
 int bt_ctf_get_field_list(const struct bt_ctf_event *ctf_event,
@@ -168,6 +179,9 @@ int bt_ctf_get_field_list(const struct bt_ctf_event *ctf_event,
                struct definition const * const **list,
                unsigned int *count)
 {
                struct definition const * const **list,
                unsigned int *count)
 {
+       if (!ctf_event || !scope || !list || !count)
+               return -EINVAL;
+
        switch (bt_ctf_field_type(scope)) {
        case CTF_TYPE_INTEGER:
        case CTF_TYPE_FLOAT:
        switch (bt_ctf_field_type(scope)) {
        case CTF_TYPE_INTEGER:
        case CTF_TYPE_FLOAT:
@@ -188,6 +202,7 @@ int bt_ctf_get_field_list(const struct bt_ctf_event *ctf_event,
                } else {
                        goto error;
                }
                } else {
                        goto error;
                }
+               break;
        }
        case CTF_TYPE_UNTAGGED_VARIANT:
                goto error;
        }
        case CTF_TYPE_UNTAGGED_VARIANT:
                goto error;
@@ -205,6 +220,7 @@ int bt_ctf_get_field_list(const struct bt_ctf_event *ctf_event,
                } else {
                        goto error;
                }
                } else {
                        goto error;
                }
+               break;
        }
        case CTF_TYPE_ARRAY:
        {
        }
        case CTF_TYPE_ARRAY:
        {
@@ -220,6 +236,7 @@ int bt_ctf_get_field_list(const struct bt_ctf_event *ctf_event,
                } else {
                        goto error;
                }
                } else {
                        goto error;
                }
+               break;
        }
        case CTF_TYPE_SEQUENCE:
        {
        }
        case CTF_TYPE_SEQUENCE:
        {
@@ -235,6 +252,7 @@ int bt_ctf_get_field_list(const struct bt_ctf_event *ctf_event,
                } else {
                        goto error;
                }
                } else {
                        goto error;
                }
+               break;
        }
        default:
                break;
        }
        default:
                break;
@@ -254,8 +272,12 @@ struct bt_context *bt_ctf_event_get_context(const struct bt_ctf_event *ctf_event
        struct bt_context *ret = NULL;
        struct ctf_file_stream *cfs;
        struct ctf_trace *trace;
        struct bt_context *ret = NULL;
        struct ctf_file_stream *cfs;
        struct ctf_trace *trace;
-       struct ctf_event_definition *event = ctf_event->parent;
+       struct ctf_event_definition *event;
 
 
+       if (!ctf_event)
+               return NULL;
+
+       event = ctf_event->parent;
        cfs = container_of(event->stream, struct ctf_file_stream,
                        parent);
        trace = cfs->parent.stream_class->trace;
        cfs = container_of(event->stream, struct ctf_file_stream,
                        parent);
        trace = cfs->parent.stream_class->trace;
@@ -270,8 +292,12 @@ int bt_ctf_event_get_handle_id(const struct bt_ctf_event *ctf_event)
        int ret = -1;
        struct ctf_file_stream *cfs;
        struct ctf_trace *trace;
        int ret = -1;
        struct ctf_file_stream *cfs;
        struct ctf_trace *trace;
-       struct ctf_event_definition *event = ctf_event->parent;
+       struct ctf_event_definition *event;
+
+       if (!ctf_event)
+               return -EINVAL;
 
 
+       event = ctf_event->parent;
        cfs = container_of(event->stream, struct ctf_file_stream,
                        parent);
        trace = cfs->parent.stream_class->trace;
        cfs = container_of(event->stream, struct ctf_file_stream,
                        parent);
        trace = cfs->parent.stream_class->trace;
@@ -283,7 +309,12 @@ int bt_ctf_event_get_handle_id(const struct bt_ctf_event *ctf_event)
 
 uint64_t bt_ctf_get_timestamp(const struct bt_ctf_event *ctf_event)
 {
 
 uint64_t bt_ctf_get_timestamp(const struct bt_ctf_event *ctf_event)
 {
-       struct ctf_event_definition *event = ctf_event->parent;
+       struct ctf_event_definition *event;
+
+       if (!ctf_event)
+               return -1ULL;
+
+       event = ctf_event->parent;
        if (event && event->stream->has_timestamp)
                return event->stream->real_timestamp;
        else
        if (event && event->stream->has_timestamp)
                return event->stream->real_timestamp;
        else
@@ -292,7 +323,12 @@ uint64_t bt_ctf_get_timestamp(const struct bt_ctf_event *ctf_event)
 
 uint64_t bt_ctf_get_cycles(const struct bt_ctf_event *ctf_event)
 {
 
 uint64_t bt_ctf_get_cycles(const struct bt_ctf_event *ctf_event)
 {
-       struct ctf_event_definition *event = ctf_event->parent;
+       struct ctf_event_definition *event;
+
+       if (!ctf_event)
+               return -1ULL;
+
+       event = ctf_event->parent;
        if (event && event->stream->has_timestamp)
                return event->stream->cycles_timestamp;
        else
        if (event && event->stream->has_timestamp)
                return event->stream->cycles_timestamp;
        else
@@ -320,7 +356,7 @@ int bt_ctf_get_int_signedness(const struct definition *field)
        if (field && bt_ctf_field_type(field) == CTF_TYPE_INTEGER) {
                ret = get_int_signedness(field);
        } else {
        if (field && bt_ctf_field_type(field) == CTF_TYPE_INTEGER) {
                ret = get_int_signedness(field);
        } else {
-               ret = -1;
+               ret = -EINVAL;
                bt_ctf_field_set_error(-EINVAL);
        }
 
                bt_ctf_field_set_error(-EINVAL);
        }
 
@@ -334,7 +370,7 @@ int bt_ctf_get_int_base(const struct definition *field)
        if (field && bt_ctf_field_type(field) == CTF_TYPE_INTEGER) {
                ret = get_int_base(field);
        } else {
        if (field && bt_ctf_field_type(field) == CTF_TYPE_INTEGER) {
                ret = get_int_base(field);
        } else {
-               ret = -1;
+               ret = -EINVAL;
                bt_ctf_field_set_error(-EINVAL);
        }
 
                bt_ctf_field_set_error(-EINVAL);
        }
 
@@ -348,7 +384,7 @@ int bt_ctf_get_int_byte_order(const struct definition *field)
        if (field && bt_ctf_field_type(field) == CTF_TYPE_INTEGER) {
                ret = get_int_byte_order(field);
        } else {
        if (field && bt_ctf_field_type(field) == CTF_TYPE_INTEGER) {
                ret = get_int_byte_order(field);
        } else {
-               ret = -1;
+               ret = -EINVAL;
                bt_ctf_field_set_error(-EINVAL);
        }
 
                bt_ctf_field_set_error(-EINVAL);
        }
 
@@ -362,7 +398,7 @@ ssize_t bt_ctf_get_int_len(const struct definition *field)
        if (field && bt_ctf_field_type(field) == CTF_TYPE_INTEGER) {
                ret = (ssize_t) get_int_len(field);
        } else {
        if (field && bt_ctf_field_type(field) == CTF_TYPE_INTEGER) {
                ret = (ssize_t) get_int_len(field);
        } else {
-               ret = -1;
+               ret = -EINVAL;
                bt_ctf_field_set_error(-EINVAL);
        }
 
                bt_ctf_field_set_error(-EINVAL);
        }
 
@@ -374,7 +410,7 @@ enum ctf_string_encoding bt_ctf_get_encoding(const struct definition *field)
        enum ctf_string_encoding ret = 0;
 
        if (!field)
        enum ctf_string_encoding ret = 0;
 
        if (!field)
-               goto end;
+               goto error;
 
        if (bt_ctf_field_type(field) == CTF_TYPE_INTEGER)
                ret = get_int_encoding(field);
 
        if (bt_ctf_field_type(field) == CTF_TYPE_INTEGER)
                ret = get_int_encoding(field);
@@ -382,8 +418,6 @@ enum ctf_string_encoding bt_ctf_get_encoding(const struct definition *field)
                ret = get_string_encoding(field);
        else
                goto error;
                ret = get_string_encoding(field);
        else
                goto error;
-
-end:
        return ret;
 
 error:
        return ret;
 
 error:
@@ -462,7 +496,7 @@ int bt_ctf_get_event_decl_list(int handle_id, struct bt_context *ctx,
        struct trace_descriptor *td;
        struct ctf_trace *tin;
 
        struct trace_descriptor *td;
        struct ctf_trace *tin;
 
-       if (!ctx)
+       if (!ctx || !list || !count)
                goto error;
 
        handle = g_hash_table_lookup(ctx->trace_handles,
                goto error;
 
        handle = g_hash_table_lookup(ctx->trace_handles,
@@ -485,6 +519,7 @@ const char *bt_ctf_get_decl_event_name(const struct bt_ctf_event_decl *event)
 {
        if (!event)
                return NULL;
 {
        if (!event)
                return NULL;
+
        return g_quark_to_string(event->parent.name);
 }
 
        return g_quark_to_string(event->parent.name);
 }
 
@@ -500,6 +535,9 @@ int bt_ctf_get_decl_fields(struct bt_ctf_event_decl *event_decl,
        int ret = 0;
        *count = 0;
 
        int ret = 0;
        *count = 0;
 
+       if (!event_decl || !list || !count)
+               return -EINVAL;
+
        switch (scope) {
        case BT_EVENT_CONTEXT:
                if (event_decl->context_decl) {
        switch (scope) {
        case BT_EVENT_CONTEXT:
                if (event_decl->context_decl) {
@@ -603,7 +641,8 @@ end:
 
 const char *bt_ctf_get_decl_field_name(const struct bt_ctf_field_decl *field)
 {
 
 const char *bt_ctf_get_decl_field_name(const struct bt_ctf_field_decl *field)
 {
-       if (field)
-               return rem_(g_quark_to_string(((struct declaration_field *) field)->name));
-       return NULL;
+       if (!field)
+               return NULL;
+
+       return rem_(g_quark_to_string(((struct declaration_field *) field)->name));
 }
 }
This page took 0.026506 seconds and 4 git commands to generate.