From b801b7b670872b8a31d11b3683b4afc3e45a07f8 Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Sat, 8 Aug 2020 07:36:34 +0200 Subject: [PATCH] fix: unterminated \-escape An assertion failed when the last character is a '\' and we're in a character or a string. Reported by Agency for Defense Development. https://lists.gnu.org/r/bug-bison/2020-08/msg00009.html * src/scan-gram.l: Catch unterminated escapes. * tests/input.at (Unexpected end of file): New. --- src/scan-gram.l | 18 ++++++++--- tests/input.at | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 106 insertions(+), 10 deletions(-) diff --git a/src/scan-gram.l b/src/scan-gram.l index f957f137..e10d68e2 100644 --- a/src/scan-gram.l +++ b/src/scan-gram.l @@ -567,6 +567,8 @@ eqopt ({sp}=)? _("POSIX Yacc does not support string literals")); RETURN_VALUE (STRING, last_string); } + <> unexpected_eof (token_start, "\""); + "\n" unexpected_newline (token_start, "\""); } @@ -580,13 +582,10 @@ eqopt ({sp}=)? _("POSIX Yacc does not support string literals")); RETURN_VALUE (TSTRING, last_string); } + <> unexpected_eof (token_start, "\")"); + "\n" unexpected_newline (token_start, "\")"); } - -{ - <> unexpected_eof (token_start, "\""); - "\n" unexpected_newline (token_start, "\""); -} /*----------------------------------------------------------. @@ -692,6 +691,15 @@ eqopt ({sp}=)? p); STRING_1GROW ('?'); } + + "\\" { + // None of the other rules matched: the last character of this + // file is "\". But Flex does not support "\\<>". + unexpected_eof (token_start, + YY_START == SC_ESCAPED_CHARACTER ? "?'" + : YY_START == SC_ESCAPED_STRING ? "?\"" + : "?\")"); + } } /*--------------------------------------------. diff --git a/tests/input.at b/tests/input.at index 915cfed6..cfa065ad 100644 --- a/tests/input.at +++ b/tests/input.at @@ -1395,11 +1395,6 @@ AT_CLEANUP AT_SETUP([Torturing the Scanner]) AT_BISON_OPTION_PUSHDEFS -AT_DATA([input.y], []) -AT_BISON_CHECK([input.y], [1], [], -[[input.y:1.1: error: unexpected end of file -]]) - AT_DATA([input.y], [{} @@ -2506,6 +2501,99 @@ input.y:5.19: error: invalid character after \-escape: \001 AT_CLEANUP +## ------------------------ ## +## Unexpected end of file. ## +## ------------------------ ## + +AT_SETUP([[Unexpected end of file]]) + + +AT_DATA([input.y], []) +AT_BISON_CHECK([-fcaret input.y], [1], [], +[[input.y:1.1: error: unexpected end of file +]]) + + +AT_DATA_NO_FINAL_EOL([char.y], +[[%token FOO ']]) + +AT_BISON_CHECK([-fcaret char.y], [1], [], +[[char.y:1.12: error: missing "'" at end of file + 1 | %token FOO ' + | ^ +char.y:1.12: error: empty character literal + 1 | %token FOO ' + | ^ +]]) + + +AT_DATA_NO_FINAL_EOL([escape-in-char.y], +[[%token FOO '\]]) + +AT_BISON_CHECK([-fcaret escape-in-char.y], [1], [], +[[escape-in-char.y:1.12-13: error: missing '?\'' at end of file + 1 | %token FOO '\ + | ^~ +escape-in-char.y:1.14: error: unexpected end of file + 1 | %token FOO '\ + | ^ +]]) + + +AT_DATA_NO_FINAL_EOL([string.y], +[[%token FOO "]]) + +AT_BISON_CHECK([-fcaret string.y], [1], [], +[[string.y:1.12: error: missing '"' at end of file + 1 | %token FOO " + | ^ +string.y:1.13: error: unexpected end of file + 1 | %token FOO " + | ^ +]]) + + +AT_DATA_NO_FINAL_EOL([escape-in-string.y], +[[%token FOO "\]]) + +AT_BISON_CHECK([-fcaret escape-in-string.y], [1], [], +[[escape-in-string.y:1.12-13: error: missing '?"' at end of file + 1 | %token FOO "\ + | ^~ +escape-in-string.y:1.14: error: unexpected end of file + 1 | %token FOO "\ + | ^ +]]) + + +AT_DATA_NO_FINAL_EOL([tstring.y], +[[%token FOO _("]]) + +AT_BISON_CHECK([-fcaret tstring.y], [1], [], +[[tstring.y:1.12-14: error: missing '")' at end of file + 1 | %token FOO _(" + | ^~~ +tstring.y:1.15: error: unexpected end of file + 1 | %token FOO _(" + | ^ +]]) + + +AT_DATA_NO_FINAL_EOL([escape-in-tstring.y], +[[%token FOO _("\]]) + +AT_BISON_CHECK([-fcaret escape-in-tstring.y], [1], [], +[[escape-in-tstring.y:1.12-15: error: missing '?")' at end of file + 1 | %token FOO _("\ + | ^~~~ +escape-in-tstring.y:1.16: error: unexpected end of file + 1 | %token FOO _("\ + | ^ +]]) + +AT_CLEANUP + + ## ------------------------- ## ## LAC: Errors for %define. ## ## ------------------------- ## -- 2.11.4.GIT