summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAkim Demaille <akim.demaille@gmail.com>2020-08-08 07:36:34 +0200
committerAkim Demaille <akim.demaille@gmail.com>2020-08-08 07:53:33 +0200
commitb801b7b670872b8a31d11b3683b4afc3e45a07f8 (patch)
tree1100410a486a4213a41a3841666b33e2fd1dad63
parentb7aab2dbad43aaf14eebe78d54aafa245a000988 (diff)
downloadbison-b801b7b670872b8a31d11b3683b4afc3e45a07f8.tar.gz
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.
-rw-r--r--src/scan-gram.l18
-rw-r--r--tests/input.at98
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);
}
+ <<EOF>> unexpected_eof (token_start, "\"");
+ "\n" unexpected_newline (token_start, "\"");
}
<SC_ESCAPED_TSTRING>
@@ -580,13 +582,10 @@ eqopt ({sp}=)?
_("POSIX Yacc does not support string literals"));
RETURN_VALUE (TSTRING, last_string);
}
+ <<EOF>> unexpected_eof (token_start, "\")");
+ "\n" unexpected_newline (token_start, "\")");
}
-<SC_ESCAPED_STRING,SC_ESCAPED_TSTRING>
-{
- <<EOF>> 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 "\\<<EOF>>".
+ 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. ##
## ------------------------- ##