Add error handling to generate io struct visitor
[babeltrace.git] / formats / ctf / metadata / ctf-visitor-generate-io-struct.c
index bdf7125d5cb31ac20f691ecf807bfc2bb401ad8d..7009d03e1ad92bf4a5b539e147290e9e95f3ee40 100644 (file)
@@ -351,20 +351,29 @@ struct declaration *ctf_type_declarator_visit(FILE *fd, int depth,
                        declaration = &array_declaration->p;
                        break;
                }
-               case NODE_INTEGER:
-               case NODE_TYPE_SPECIFIER:
+               case NODE_TYPE_SPECIFIER_LIST:
                {
                        struct declaration_sequence *sequence_declaration;
                        struct declaration_integer *integer_declaration;
-                       GQuark dummy_id;
 
-                       declaration = ctf_type_declarator_visit(fd, depth,
-                                               length,
-                                               &dummy_id, NULL,
-                                               declaration_scope,
-                                               NULL, trace);
-                       assert(declaration->id == CTF_TYPE_INTEGER);
+                       declaration = ctf_type_specifier_list_visit(fd, depth,
+                               length, declaration_scope, trace);
+                       if (!declaration) {
+                               fprintf(fd, "[error] %s: unable to find declaration type for sequence length\n", __func__);
+                               return NULL;
+                       }
+                       if (declaration->id != CTF_TYPE_INTEGER) {
+                               fprintf(fd, "[error] %s: length type for sequence is expected to be an integer (unsigned).\n", __func__);
+                               declaration_unref(declaration);
+                               return NULL;
+                       }
                        integer_declaration = container_of(declaration, struct declaration_integer, p);
+                       if (integer_declaration->signedness != false) {
+                               fprintf(fd, "[error] %s: length type for sequence should always be an unsigned integer.\n", __func__);
+                               declaration_unref(declaration);
+                               return NULL;
+                       }
+
                        sequence_declaration = sequence_declaration_new(integer_declaration,
                                        nested_declaration, declaration_scope);
                        declaration = &sequence_declaration->p;
@@ -402,6 +411,10 @@ int ctf_struct_type_declarators_visit(FILE *fd, int depth,
                                                &field_name, iter,
                                                struct_declaration->scope,
                                                NULL, trace);
+               if (!field_declaration) {
+                       fprintf(fd, "[error] %s: unable to find struct field declaration type\n", __func__);
+                       return -EINVAL;
+               }
                struct_declaration_add_field(struct_declaration,
                                             g_quark_to_string(field_name),
                                             field_declaration);
@@ -428,6 +441,10 @@ int ctf_variant_type_declarators_visit(FILE *fd, int depth,
                                                &field_name, iter,
                                                untagged_variant_declaration->scope,
                                                NULL, trace);
+               if (!field_declaration) {
+                       fprintf(fd, "[error] %s: unable to find variant field declaration type\n", __func__);
+                       return -EINVAL;
+               }
                untagged_variant_declaration_add_field(untagged_variant_declaration,
                                              g_quark_to_string(field_name),
                                              field_declaration);
@@ -452,6 +469,19 @@ int ctf_typedef_visit(FILE *fd, int depth, struct declaration_scope *scope,
                                        type_specifier_list,
                                        &identifier, iter,
                                        scope, NULL, trace);
+               if (!type_declaration) {
+                       fprintf(fd, "[error] %s: problem creating type declaration\n", __func__);
+                       return -EINVAL;
+               }
+               /*
+                * Don't allow typedef and typealias of untagged
+                * variants.
+                */
+               if (type_declaration->id == CTF_TYPE_UNTAGGED_VARIANT) {
+                       fprintf(fd, "[error] %s: typedef of untagged variant is not permitted.\n", __func__);
+                       declaration_unref(type_declaration);
+                       return -EPERM;
+               }
                ret = register_declaration(identifier, type_declaration, scope);
                if (ret) {
                        type_declaration->declaration_free(type_declaration);
@@ -492,6 +522,15 @@ int ctf_typealias_visit(FILE *fd, int depth, struct declaration_scope *scope,
                err = -EINVAL;
                goto error;
        }
+       /*
+        * Don't allow typedef and typealias of untagged
+        * variants.
+        */
+       if (type_declaration->id == CTF_TYPE_UNTAGGED_VARIANT) {
+               fprintf(fd, "[error] %s: typedef of untagged variant is not permitted.\n", __func__);
+               declaration_unref(type_declaration);
+               return -EPERM;
+       }
        /*
         * The semantic validator does not check whether the target is
         * abstract or not (if it has an identifier). Check it here.
@@ -719,7 +758,7 @@ struct declaration *ctf_declaration_variant_visit(FILE *fd,
                return &variant_declaration->p;
        }
 error:
-       untagged_variant_declaration->p.declaration_free(&variant_declaration->p);
+       untagged_variant_declaration->p.declaration_free(&untagged_variant_declaration->p);
        return NULL;
 }
 
@@ -845,15 +884,19 @@ struct declaration *ctf_declaration_enum_visit(FILE *fd, int depth,
                        }
                }
                if (!container_type) {
-                       fprintf(fd, "[error] %s: missing container type for enumeration\n", __func__);
-                       return NULL;
-                       
+                       declaration = lookup_declaration(g_quark_from_static_string("int"),
+                                                        declaration_scope);
+                       if (!declaration) {
+                               fprintf(fd, "[error] %s: \"int\" type declaration missing for enumeration\n", __func__);
+                               return NULL;
+                       }
+               } else {
+                       declaration = ctf_type_declarator_visit(fd, depth,
+                                               container_type,
+                                               &dummy_id, NULL,
+                                               declaration_scope,
+                                               NULL, trace);
                }
-               declaration = ctf_type_declarator_visit(fd, depth,
-                                       container_type,
-                                       &dummy_id, NULL,
-                                       declaration_scope,
-                                       NULL, trace);
                if (!declaration) {
                        fprintf(fd, "[error] %s: unable to create container type for enumeration\n", __func__);
                        return NULL;
@@ -1177,7 +1220,7 @@ struct declaration *ctf_type_specifier_list_visit(FILE *fd,
                        &node->u.integer.expressions, trace);
        case TYPESPEC_STRING:
                return ctf_declaration_string_visit(fd, depth,
-                       &first->u.string.expressions, trace);
+                       &node->u.string.expressions, trace);
        case TYPESPEC_STRUCT:
                return ctf_declaration_struct_visit(fd, depth,
                        node->u._struct.name,
@@ -1788,6 +1831,10 @@ int ctf_visitor_construct_metadata(FILE *fd, int depth, struct ctf_node *node,
                                return ret;
                        }
                }
+               if (!trace->streams) {
+                       fprintf(fd, "[error] %s: missing trace declaration\n", __func__);
+                       return -EINVAL;
+               }
                cds_list_for_each_entry(iter, &node->u.root.stream, siblings) {
                        ret = ctf_stream_visit(fd, depth + 1, iter,
                                               trace->root_declaration_scope, trace);
This page took 0.026465 seconds and 4 git commands to generate.