From ffc167572a9d56c52908aebefe62ba3e3e2a9cf8 Mon Sep 17 00:00:00 2001 From: Jeremy Kerr Date: Thu, 26 Sep 2013 15:45:58 +0800 Subject: [PATCH 1/1] discover/grub2: Clean up error-handling for grub2 parser & lexer Rather than printf() & exit(), use the pb logging functions and abort the parse. Signed-off-by: Jeremy Kerr --- discover/grub2/lexer.l | 7 ++++++- discover/grub2/parser.y | 25 +++++++++++-------------- discover/grub2/script.c | 3 ++- test/parser/Makefile.am | 2 ++ test/parser/test-grub2-lexer-error.c | 13 +++++++++++++ test/parser/test-grub2-parser-error.c | 13 +++++++++++++ 6 files changed, 47 insertions(+), 16 deletions(-) create mode 100644 test/parser/test-grub2-lexer-error.c create mode 100644 test/parser/test-grub2-parser-error.c diff --git a/discover/grub2/lexer.l b/discover/grub2/lexer.l index 0558ed9..e1aad99 100644 --- a/discover/grub2/lexer.l +++ b/discover/grub2/lexer.l @@ -3,6 +3,8 @@ #include "grub2.h" #include "parser.h" #include + +void yyerror(struct grub2_parser *parser, const char *fmt, ...); %} %option nounput noinput @@ -118,7 +120,10 @@ VARNAME ([[:alpha:]][_[:alnum:]]*|[0-9]|[\?@\*#]) /* strip comments */ #.* ; -. printf("unknown token '%s'\n", yytext); exit(1); +. { + yyerror(yyget_extra(yyscanner), "unknown token '%s'\n", yytext); + yyterminate(); + } %% diff --git a/discover/grub2/parser.y b/discover/grub2/parser.y index 02ca7b2..8ab17a6 100644 --- a/discover/grub2/parser.y +++ b/discover/grub2/parser.y @@ -6,17 +6,15 @@ %{ #include +#include #include "grub2.h" #include "parser.h" #include "lexer.h" -static void print_token(FILE *fp, int type, YYSTYPE value); - #define YYLEX_PARAM parser->scanner -#define YYPRINT(f, t, v) print_token(f, t, v) -static void yyerror(struct grub2_parser *, char const *s); +void yyerror(struct grub2_parser *parser, const char *fmt, ...); %} %union { @@ -148,18 +146,17 @@ word: TOKEN_WORD } %% -void yyerror(struct grub2_parser *parser, char const *s) +void yyerror(struct grub2_parser *parser, const char *fmt, ...) { - fprintf(stderr, "%d: error: %s '%s'\n", - yyget_lineno(parser->scanner), - s, yyget_text(parser->scanner)); -} + const char *str; + va_list ap; -static void print_token(FILE *fp, int type, YYSTYPE value) -{ - if (type != TOKEN_WORD) - return; - fprintf(fp, "%s", value.word->text); + va_start(ap, fmt); + str = talloc_vasprintf(parser, fmt, ap); + va_end(ap); + + pb_log("parse error: %d('%s'): %s\n", yyget_lineno(parser->scanner), + yyget_text(parser->scanner), str); } struct grub2_statements *create_statements(struct grub2_parser *parser) diff --git a/discover/grub2/script.c b/discover/grub2/script.c index 0cf2196..d0b824a 100644 --- a/discover/grub2/script.c +++ b/discover/grub2/script.c @@ -3,6 +3,7 @@ #include #include +#include #include #include @@ -278,7 +279,7 @@ int statement_simple_execute(struct grub2_script *script, entry = script_lookup_function(script, st->argv->argv[0]); if (!entry) { - fprintf(stderr, "undefined function '%s'\n", st->argv->argv[0]); + pb_log("grub2: undefined function '%s'\n", st->argv->argv[0]); return 1; } diff --git a/test/parser/Makefile.am b/test/parser/Makefile.am index 8159bd6..e7146c5 100644 --- a/test/parser/Makefile.am +++ b/test/parser/Makefile.am @@ -35,6 +35,8 @@ TESTS = \ test-grub2-single-line-if \ test-grub2-f18-ppc64 \ test-grub2-ubuntu-13_04-x86 \ + test-grub2-lexer-error \ + test-grub2-parser-error \ test-kboot-single \ test-yaboot-single \ test-yaboot-partition \ diff --git a/test/parser/test-grub2-lexer-error.c b/test/parser/test-grub2-lexer-error.c new file mode 100644 index 0000000..503ec99 --- /dev/null +++ b/test/parser/test-grub2-lexer-error.c @@ -0,0 +1,13 @@ + +#include "parser-test.h" + +#if 0 /* PARSER_EMBEDDED_CONFIG */ +& +#endif + +void run_test(struct parser_test *test) +{ + test_read_conf_embedded(test); + test_run_parser(test, "grub2"); + check_boot_option_count(test->ctx, 0); +} diff --git a/test/parser/test-grub2-parser-error.c b/test/parser/test-grub2-parser-error.c new file mode 100644 index 0000000..a7a00a4 --- /dev/null +++ b/test/parser/test-grub2-parser-error.c @@ -0,0 +1,13 @@ + +#include "parser-test.h" + +#if 0 /* PARSER_EMBEDDED_CONFIG */ +{ +#endif + +void run_test(struct parser_test *test) +{ + test_read_conf_embedded(test); + test_run_parser(test, "grub2"); + check_boot_option_count(test->ctx, 0); +} -- 2.39.2