From be688a4bde5b30e7f95afd5e5bf95deae749760c Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Wed, 19 Feb 2025 19:12:38 +0000 Subject: [PATCH] parser: Properly handle yyparse() return codes This patch converts them into our usual errno codes. Signed-off-by: Michael Tremer --- src/pakfire/parser/grammar.y | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/src/pakfire/parser/grammar.y b/src/pakfire/parser/grammar.y index 9d94539e2..8f2f847ae 100644 --- a/src/pakfire/parser/grammar.y +++ b/src/pakfire/parser/grammar.y @@ -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) -- 2.39.5