]> git.ipfire.org Git - pakfire.git/commitdiff
parser: Properly handle yyparse() return codes
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 19 Feb 2025 19:12:38 +0000 (19:12 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 19 Feb 2025 19:12:38 +0000 (19:12 +0000)
This patch converts them into our usual errno codes.

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

index 9d94539e28e050647c8f8c2b87cf9417b1dc79b0..8f2f847aed9bd43ce8d303c3473bd87f83c75aa4 100644 (file)
@@ -414,6 +414,7 @@ int pakfire_parser_parse_data(struct pakfire_parser* parent, const char* data, s
        struct pakfire* pakfire = pakfire_parser_get_pakfire(parent);
        char* dump = NULL;
        yyscan_t scanner;
+       int r;
 
        // Initialize the parser's state
        struct pakfire_parser_state state = {
@@ -440,20 +441,35 @@ int pakfire_parser_parse_data(struct pakfire_parser* parent, const char* data, s
        struct pakfire_parser* parser = NULL;
 
        YY_BUFFER_STATE buffer = yy_scan_bytes(data, len, scanner);
-       int r = yyparse(scanner, ctx, pakfire, &parser, parent, error);
+       r = yyparse(scanner, ctx, pakfire, &parser, parent, error);
        yy_delete_buffer(buffer, scanner);
 
+       // Deal with the return code of the parser
+       switch (r) {
+               // Success!
+               case 0:
+                       break;
+
+               // Aborted, usually because of a syntax error
+               case 1:
+                       r = -EBADMSG;
+                       goto ERROR;
+
+               // OOM
+               case 2:
+                       r = -ENOMEM;
+                       goto ERROR;
+
+               // Anything else should never happen
+               default:
+                       abort();
+       }
+
        // If everything was parsed successfully, we merge the sub-parser into
        // the parent parser. That way, it will be untouched if something could
        // not be successfully parsed.
-       if (r == 0) {
-               if (parser)
-                       pakfire_parser_merge(parent, parser);
-       }
-
-       // Destroy the parser
        if (parser)
-                pakfire_parser_unref(parser);
+               pakfire_parser_merge(parent, parser);
 
 #ifdef ENABLE_DEBUG
        // Save end time
@@ -469,9 +485,11 @@ int pakfire_parser_parse_data(struct pakfire_parser* parent, const char* data, s
                (double)(t_end - t_start) * 1000 / CLOCKS_PER_SEC);
 #endif
 
-       // Cleanup
+ERROR:
        if (dump)
                free(dump);
+       if (parser)
+               pakfire_parser_unref(parser);
        if (pakfire)
                pakfire_unref(pakfire);
        if (ctx)