From 76a65d59580799293a252e578ca29ebeffbc2d40 Mon Sep 17 00:00:00 2001 From: Cary R Date: Mon, 4 Sep 2023 12:37:06 -0700 Subject: [PATCH] Add error message for bad line directives to ivlpp --- ivlpp/lexor.lex | 48 +++++++++++++++++++++++++++++++++++++----------- lexor.lex | 9 ++++----- 2 files changed, 41 insertions(+), 16 deletions(-) diff --git a/ivlpp/lexor.lex b/ivlpp/lexor.lex index 653e5c1a9..9cca5aff9 100644 --- a/ivlpp/lexor.lex +++ b/ivlpp/lexor.lex @@ -728,21 +728,25 @@ keywords (line|include|define|undef|ifdef|ifndef|else|elsif|endif) */ static void handle_line_directive(void) { - char *cp; char *cpr; - unsigned long lineno; - char*fn_start; - char*fn_end; - /* Skip any leading space. */ - cp = strchr(yytext, '`'); + char *cp = strchr(yytext, '`'); /* Skip the `line directive. */ assert(strncmp(cp, "`line", 5) == 0); cp += 5; - /* strtoul skips leading space. */ - lineno = strtoul(cp, &cpr, 10); + /* strtol skips leading space. */ + long lineno = strtol(cp, &cpr, 10); if (cp == cpr || lineno == 0) { + emit_pathline(istack); + fprintf(stderr, "error: Invalid line number for `line directive.\n"); + error_count += 1; + return; + } + if (lineno < 1) { + emit_pathline(istack); + fprintf(stderr, "error: Line number for `line directive most be greater than zero.\n"); + error_count += 1; return; } cp = cpr; @@ -750,20 +754,30 @@ static void handle_line_directive(void) /* Skip the space between the line number and the file name. */ cpr += strspn(cp, " \t"); if (cp == cpr) { + emit_pathline(istack); + fprintf(stderr, "error: Invalid `line directive (missing space " + "after line number).\n"); + error_count += 1; return; } cp = cpr; /* Find the starting " and skip it. */ - fn_start = strchr(cp, '"'); + char *fn_start = strchr(cp, '"'); if (cp != fn_start) { + emit_pathline(istack); + fprintf(stderr, "error: Invalid `line directive (file name start).\n"); + error_count += 1; return; } fn_start += 1; /* Find the last ". */ - fn_end = strrchr(fn_start, '"'); + char *fn_end = strrchr(fn_start, '"'); if (!fn_end) { + emit_pathline(istack); + fprintf(stderr, "error: Invalid `line directive (file name end).\n"); + error_count += 1; return; } @@ -772,19 +786,31 @@ static void handle_line_directive(void) cpr = cp; cpr += strspn(cp, " \t"); if (cp == cpr) { + emit_pathline(istack); + fprintf(stderr, "error: Invalid `line directive (missing space " + "after file name).\n"); + error_count += 1; return; } cp = cpr; /* Check that the level is correct, we do not need the level. */ if (strspn(cp, "012") != 1) { + emit_pathline(istack); + fprintf(stderr, "error: Invalid level for `line directive\n"); + error_count += 1; return; } cp += 1; /* Verify that only space is left. */ cp += strspn(cp, " \t"); - if ((size_t)(cp-yytext) != strlen(yytext)) { + if (strncmp(cp, "//", 2) != 0 && + (size_t)(cp-yytext) != strlen(yytext)) { + emit_pathline(istack); + fprintf(stderr, "error: Invalid `line directive (extra garbage " + "after level).\n"); + error_count += 1; return; } diff --git a/lexor.lex b/lexor.lex index 14be274b4..22851ee29 100644 --- a/lexor.lex +++ b/lexor.lex @@ -1451,7 +1451,7 @@ static void line_directive() cp += strspn(cp, " \t"); /* Find the starting " and skip it. */ - char*fn_start = strchr(cp, '"'); + char *fn_start = strchr(cp, '"'); if (cp != fn_start) { VLerror(yylloc, "error: Invalid #line directive (file name start)."); return; @@ -1459,7 +1459,7 @@ static void line_directive() fn_start += 1; /* Find the last ". */ - char*fn_end = strrchr(fn_start, '"'); + char *fn_end = strrchr(fn_start, '"'); if (!fn_end) { VLerror(yylloc, "error: Invalid #line directive (file name end)."); return; @@ -1530,7 +1530,6 @@ static void line_directive2() VLerror(yylloc, "error: Line number for `line directive most be greater than zero."); return; } - lineno -= 1; cp = cpr; /* Skip the space between the line number and the file name. */ @@ -1543,7 +1542,7 @@ static void line_directive2() cp = cpr; /* Find the starting " and skip it. */ - char*fn_start = strchr(cp, '"'); + char *fn_start = strchr(cp, '"'); if (cp != fn_start) { VLerror(yylloc, "error: Invalid `line directive (file name start)."); return; @@ -1590,7 +1589,7 @@ static void line_directive2() buf[fn_end-fn_start] = 0; yylloc.text = set_file_name(buf); - yylloc.first_line = lineno; + yylloc.first_line = lineno-1; } /*