From 28c48a5e1846f5d4de7c6fc56b1e026baff6b894 Mon Sep 17 00:00:00 2001 From: Alex Elder Date: Fri, 1 Oct 2021 18:23:11 -0500 Subject: [PATCH] parser: report input read errors In qmi_parse(), the main loop accepts tokens until it sees one with id 0. If an unrecognized (or unexpected) token is encountered, it reports an error and exits using yyerror(). Normally, input() returns the next character to be processed while parsing. If the read() call in input() encounters an error, input() will return -1 instead (which happens to be EOF). The only caller of input() is yylex(). It recognizes tokens comprised of alphanumeric characters and '_'. If input() returns any other value, it uses that value as the token identifier. And in particular, if input() returns -1, that will be the value stored in the id field of the token structure yylex() returns. So, back to the top, qmi_parse() will not consider -1 a valid token id, and will report the read error as an "unexpected symbol". Instead, report a read error immediately when it occurs and exit. Move the definition of yyerror() earlier in the source file so it can be called from yylex() (and all other functions) without needing a declaration. Signed-off-by: Alex Elder Message-Id: <20211001232338.769309-8-elder@linaro.org> Signed-off-by: Bjorn Andersson --- parser.c | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/parser.c b/parser.c index a1b275f..c94da50 100644 --- a/parser.c +++ b/parser.c @@ -1,6 +1,7 @@ #include #include #include +#include #include #include #include @@ -42,6 +43,21 @@ static unsigned scratch_pos; static int yyline = 1; +static void yyerror(const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + + printf("parse error on line %u:\n\t", yyline); + vprintf(fmt, ap); + printf("\n"); + + va_end(ap); + + exit(1); +} + static int input() { static char input_buf[128]; @@ -61,8 +77,11 @@ static int input() } ret = read(0, input_buf, sizeof(input_buf)); - if (ret <= 0) - return ret; + if (ret <= 0) { + if (ret < 0) + yyerror("read error: %s", strerror(errno)); + return 0; /* End of input */ + } input_pos = 0; input_len = ret; @@ -181,21 +200,6 @@ static struct token yylex() static struct token curr_token; -static void yyerror(const char *fmt, ...) -{ - va_list ap; - - va_start(ap, fmt); - - printf("parse error on line %u:\n\t", yyline); - vprintf(fmt, ap); - printf("\n"); - - va_end(ap); - - exit(1); -} - static void token_init(void) { curr_token = yylex();