]> git.ipfire.org Git - pakfire.git/commitdiff
parser: Attempt to fix refcounting problems
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 4 May 2021 13:01:13 +0000 (13:01 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 4 May 2021 13:01:13 +0000 (13:01 +0000)
This is still leaking some references and I cannot bloody find out why

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/parser/grammar.y

index 85c347a154b7b77af8265089d22c6566401c1abf..6a9a34753a4613ab00dfb6d1e819c4417a6e3e8c 100644 (file)
@@ -164,7 +164,18 @@ static void pakfire_parser_free_declaration(struct pakfire_parser_declaration* d
        struct pakfire_parser_declaration* declaration;
 }
 
-%destructor { pakfire_parser_unref($$); } <parser>
+%initial-action {
+       *parser = pakfire_parser_create(pakfire, parent, NULL, 0);
+       if (!*parser)
+               ABORT;
+
+       // This is now the new parent parser
+       if (parent)
+               pakfire_parser_unref(parent);
+       parent = pakfire_parser_ref(*parser);
+};
+
+%destructor { if ($$) pakfire_parser_unref($$); } <parser>
 %destructor { pakfire_parser_free_declaration($$); } <declaration>
 
 %start grammar
@@ -173,16 +184,7 @@ static void pakfire_parser_free_declaration(struct pakfire_parser_declaration* d
 
 grammar                                                : %empty
                                                        {
-                                                               $$ = pakfire_parser_create(pakfire, parent, NULL, 0);
-                                                               if (!$$)
-                                                                       ABORT;
-
-                                                               // Make this the root
-                                                               if (!*parser)
-                                                                       *parser = pakfire_parser_ref($$);
-
-                                                               // This is now the new parent parser
-                                                               parent = $$;
+                                                               $$ = pakfire_parser_ref(parent);
                                                        }
                                                        | grammar declaration
                                                        {
@@ -202,9 +204,6 @@ grammar                                             : %empty
                                                                pakfire_parser_unref($2);
                                                                if (r)
                                                                        ABORT;
-
-                                                               // Go back to the parent parser
-                                                               parent = $$;
                                                        }
                                                        | grammar if_stmt
                                                        {
@@ -216,9 +215,6 @@ grammar                                             : %empty
                                                                        if (r)
                                                                                ABORT;
                                                                }
-
-                                                               // Go back to the parent parser
-                                                               parent = $$;
                                                        }
                                                        | grammar empty
                                                        {
@@ -226,12 +222,33 @@ grammar                                           : %empty
                                                        }
                                                        ;
 
-subgrammar                                     : T_INDENT grammar T_OUTDENT
+subgrammar_open                                : T_INDENT
                                                        {
-                                                               $$ = $2;
+                                                               // Create a new parser and make it parent
+                                                               PakfireParser p = pakfire_parser_create(pakfire, parent, NULL, 0);
+                                                               if (!p)
+                                                                       ABORT;
 
-                                                               // Go back to the parent parser
-                                                               parent = $$;
+                                                               if (parent)
+                                                                       pakfire_parser_unref(parent);
+                                                               parent = p;
+                                                       }
+                                                       ;
+
+subgrammar_close                       : T_OUTDENT
+                                                       {
+                                                               // Move the parent pointer back
+                                                               PakfireParser p = pakfire_parser_get_parent(parent);
+
+                                                               if (parent)
+                                                                       pakfire_parser_unref(parent);
+                                                               parent = p;
+                                                       }
+                                                       ;
+
+subgrammar                                     : subgrammar_open grammar subgrammar_close
+                                                       {
+                                                               $$ = $2;
                                                        }
                                                        ;
 
@@ -399,7 +416,7 @@ int pakfire_parser_parse_data(PakfireParser parent, const char* data, size_t len
 
        // Destroy the parser
        if (parser)
-               pakfire_parser_unref(parser);
+                pakfire_parser_unref(parser);
 
 #ifdef ENABLE_DEBUG
        // Save end time