summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--lexer.c10
-rw-r--r--tests/custom/03_bugs/18_hang_on_line_comments_at_eof31
2 files changed, 38 insertions, 3 deletions
diff --git a/lexer.c b/lexer.c
index 616f484..d9d657e 100644
--- a/lexer.c
+++ b/lexer.c
@@ -359,9 +359,6 @@ parse_comment(uc_lexer *lex)
const char *ptr, *end;
size_t elen;
- if (!buf_remaining(lex))
- return emit_op(lex, lex->lastoff, TK_ERROR, ucv_string_new("Unterminated comment"));
-
if (!strcmp(tok->u.pat, "//")) {
end = "\n";
elen = 1;
@@ -381,6 +378,13 @@ parse_comment(uc_lexer *lex)
buf_consume(lex, ptr - lex->bufstart);
+ if (lex->eof) {
+ lex->state = UT_LEX_EOF;
+
+ if (elen == 2)
+ return emit_op(lex, lex->lastoff, TK_ERROR, ucv_string_new("Unterminated comment"));
+ }
+
return NULL;
}
diff --git a/tests/custom/03_bugs/18_hang_on_line_comments_at_eof b/tests/custom/03_bugs/18_hang_on_line_comments_at_eof
new file mode 100644
index 0000000..957ed47
--- /dev/null
+++ b/tests/custom/03_bugs/18_hang_on_line_comments_at_eof
@@ -0,0 +1,31 @@
+When parsing a comment near EOF, or a comment escaping the end
+of an expression block, the lexer did end up in an infinite loop.
+
+-- Expect stderr --
+Syntax error: Expecting expression
+In line 1, byte 9:
+
+ `{{ // }}`
+ ^-- Near here
+
+
+-- End --
+
+-- Testcase --
+{{ // }}
+-- End --
+
+
+-- Expect stderr --
+Syntax error: Unterminated comment
+In line 1, byte 4:
+
+ `{{ /* }}`
+ ^-- Near here
+
+
+-- End --
+
+-- Testcase --
+{{ /* }}
+-- End --