Skip to content

Commit cc32334

Browse files
authored
Merge pull request #498 from ucode-lang/fix/unterminated-placeholder
compiler: fix endless loop on syntax errors in template placeholders
2 parents 20eb2ca + 96cdb45 commit cc32334

2 files changed

Lines changed: 58 additions & 1 deletion

File tree

‎compiler.c‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,15 @@ uc_compiler_set_srcpos(uc_compiler_t *compiler, size_t srcpos)
237237
size_t delta;
238238

239239
/* ensure that lines counts are strictly increasing */
240-
assert(srcpos == 0 || srcpos >= compiler->current_srcpos);
240+
assert(srcpos == 0 || srcpos >= compiler->current_srcpos ||
241+
compiler->parser->error);
242+
243+
/* after a syntax error, code may be emitted for tokens in front of the
244+
* current position, e.g. after the expression of a template literal
245+
* placeholder has been terminated prematurely. The generated code is
246+
* discarded, but a negative offset would be taken for an enormous one */
247+
if (srcpos < compiler->current_srcpos)
248+
srcpos = 0;
241249

242250
delta = srcpos ? srcpos - compiler->current_srcpos : 0;
243251
compiler->current_srcpos += delta;
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
A syntax error within the expression of a template literal placeholder
2+
made the compiler spin until it ran out of memory. The statement
3+
terminating the placeholder expression had advanced the source position
4+
beyond the last token of the expression, and instructions emitted for
5+
that token afterwards moved the position backwards. The negative offset
6+
was taken for an enormous one when recording the source position of the
7+
instruction, which then took practically forever and a lot of memory.
8+
9+
1. Excess token in a placeholder.
10+
11+
-- Testcase --
12+
x = `a${ 1 2 }`;
13+
-- End --
14+
15+
-- Args --
16+
-R
17+
-- End --
18+
19+
-- Expect stderr --
20+
Syntax error: Unexpected token
21+
Expecting '}'
22+
In line 1, byte 12:
23+
24+
`x = `a${ 1 2 }`;`
25+
~~~~~~~~~~~^
26+
27+
28+
-- End --
29+
30+
31+
2. Unterminated placeholder at the end of the source.
32+
33+
-- Testcase --
34+
x = `tpl${
35+
-- End --
36+
37+
-- Args --
38+
-R
39+
-- End --
40+
41+
-- Expect stderr --
42+
Syntax error: Expecting expression
43+
In line 2, byte 1:
44+
45+
`x = `tpl${`
46+
^-- Near here
47+
48+
49+
-- End --

0 commit comments

Comments
 (0)