Skip to content

Commit f3afc5f

Browse files
committed
Improve 'end of input' SyntaxError handling
* Detect end of input more reliably based on lexer window. This helps detect truncated tokens, such as in eval('"foo bar').
1 parent 8ab1db2 commit f3afc5f

1 file changed

Lines changed: 22 additions & 5 deletions

File tree

src-input/duk_error_augment.c

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -433,11 +433,28 @@ DUK_LOCAL void duk__add_compiler_error_line(duk_hthread *thr) {
433433
(duk_tval *) duk_get_tval(thr, -1)));
434434

435435
if (duk_get_prop_stridx_short(thr, -1, DUK_STRIDX_MESSAGE)) {
436-
if (thr->compile_ctx->curr_token.start_offset + 1 >= thr->compile_ctx->lex.input_length) {
437-
duk_push_sprintf(thr, " (line %ld, end of input)", (long) thr->compile_ctx->curr_token.start_line);
438-
} else {
439-
duk_push_sprintf(thr, " (line %ld)", (long) thr->compile_ctx->curr_token.start_line);
440-
}
436+
duk_bool_t at_end;
437+
438+
/* Best guesstimate that error occurred at end of input, token
439+
* truncated by end of input, etc.
440+
*/
441+
#if 0
442+
at_end = (thr->compile_ctx->curr_token.start_offset + 1 >= thr->compile_ctx->lex.input_length);
443+
at_end = (thr->compile_ctx->lex.window[0].codepoint < 0 || thr->compile_ctx->lex.window[1].codepoint < 0);
444+
#endif
445+
at_end = (thr->compile_ctx->lex.window[0].codepoint < 0);
446+
447+
DUK_D(DUK_DPRINT("syntax error, determined at_end=%ld; curr_token.start_offset=%ld, "
448+
"lex.input_length=%ld, window[0].codepoint=%ld, window[1].codepoint=%ld",
449+
(long) at_end,
450+
(long) thr->compile_ctx->curr_token.start_offset,
451+
(long) thr->compile_ctx->lex.input_length,
452+
(long) thr->compile_ctx->lex.window[0].codepoint,
453+
(long) thr->compile_ctx->lex.window[1].codepoint));
454+
455+
duk_push_sprintf(thr, " (line %ld%s)",
456+
(long) thr->compile_ctx->curr_token.start_line,
457+
at_end ? ", end of input" : "");
441458
duk_concat(thr, 2);
442459
duk_put_prop_stridx_short(thr, -2, DUK_STRIDX_MESSAGE);
443460
} else {

0 commit comments

Comments
 (0)