Handle make_node errors with TLS dummy node
[babeltrace.git] / formats / ctf / metadata / ctf-parser.y
index a31e9637e24b930d8060fffe988035a282d2e6bf..5d2415c7d6a03c47b841d451c808fd719b9fc9f6 100644 (file)
@@ -83,6 +83,11 @@ static const char *node_type_to_str[] = {
 #undef ENTRY
 };
 
+/* Static node for out of memory errors */
+static __thread struct ctf_node error_node = {
+       .type = NODE_ERROR,
+};
+
 BT_HIDDEN
 const char *node_type(struct ctf_node *node)
 {
@@ -109,41 +114,6 @@ static struct gc_string *gc_string_alloc(struct ctf_scanner *scanner,
        return gstr;
 }
 
-/*
- * note: never use gc_string_append on a string that has external references.
- * gsrc will be garbage collected immediately, and gstr might be.
- * Should only be used to append characters to a string literal or constant.
- */
-BT_HIDDEN
-struct gc_string *gc_string_append(struct ctf_scanner *scanner,
-                                  struct gc_string *gstr,
-                                  struct gc_string *gsrc)
-{
-       size_t newlen = strlen(gsrc->s) + strlen(gstr->s) + 1;
-       size_t alloclen;
-
-       /* TODO: could be faster with find first bit or glib Gstring */
-       /* sizeof long to account for malloc header (int or long ?) */
-       for (alloclen = 8; alloclen < sizeof(long) + sizeof(*gstr) + newlen;
-            alloclen *= 2);
-
-       if (alloclen > gstr->alloclen) {
-               struct gc_string *newgstr;
-
-               newgstr = gc_string_alloc(scanner, newlen);
-               strcpy(newgstr->s, gstr->s);
-               strcat(newgstr->s, gsrc->s);
-               bt_list_del(&gstr->gc);
-               free(gstr);
-               gstr = newgstr;
-       } else {
-               strcat(gstr->s, gsrc->s);
-       }
-       bt_list_del(&gsrc->gc);
-       free(gsrc);
-       return gstr;
-}
-
 void setstring(struct ctf_scanner *scanner, YYSTYPE *lvalp, const char *src)
 {
        lvalp->gs = gc_string_alloc(scanner, strlen(src) + 1);
@@ -362,8 +332,11 @@ static struct ctf_node *make_node(struct ctf_scanner *scanner,
        struct ctf_node *node;
 
        node = malloc(sizeof(*node));
-       if (!node)
-               return NULL;
+       if (!node) {
+               error_node.lineno = yyget_lineno(scanner->scanner);
+               printfl_fatal(error_node.lineno, "out of memory");
+               return &error_node;
+       }
        memset(node, 0, sizeof(*node));
        node->type = type;
        node->lineno = yyget_lineno(scanner->scanner);
@@ -373,6 +346,7 @@ static struct ctf_node *make_node(struct ctf_scanner *scanner,
 
        switch (type) {
        case NODE_ROOT:
+               node->type = NODE_ERROR;
                printfn_fatal(node, "trying to create root node");
                break;
 
@@ -453,6 +427,7 @@ static struct ctf_node *make_node(struct ctf_scanner *scanner,
 
        case NODE_UNKNOWN:
        default:
+               node->type = NODE_ERROR;
                printfn_fatal(node, "unknown node type '%d'", (int) type);
                break;
        }
@@ -1234,9 +1209,7 @@ postfix_expression:
                }
        |       LPAREN unary_expression RPAREN
                {
-                       $$ = make_node(scanner, NODE_UNARY_EXPRESSION);
-                       $$->u.unary_expression.type = UNARY_NESTED;
-                       $$->u.unary_expression.u.nested_exp = $2;
+                       $$ = $2;
                }
        |       postfix_expression LSBRAC unary_expression RSBRAC
                {
This page took 0.023806 seconds and 4 git commands to generate.