Skip to content

Commit 0cb2ef1

Browse files
committed
Fix code paths that still conflated null characters with EOF
1 parent 47a9260 commit 0cb2ef1

File tree

4 files changed

+15
-4
lines changed

4 files changed

+15
-4
lines changed

cli/src/tests/parser_test.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,17 @@ fn test_parsing_invalid_chars_at_eof() {
266266
assert_eq!(tree.root_node().to_sexp(), "(ERROR (UNEXPECTED INVALID))");
267267
}
268268

269+
#[test]
270+
fn test_parsing_unexpected_null_characters_within_source() {
271+
let mut parser = Parser::new();
272+
parser.set_language(get_language("javascript")).unwrap();
273+
let tree = parser.parse(b"var \0 something;", None).unwrap();
274+
assert_eq!(
275+
tree.root_node().to_sexp(),
276+
"(program (variable_declaration (ERROR (UNEXPECTED '\\0')) (variable_declarator name: (identifier))))"
277+
);
278+
}
279+
269280
#[test]
270281
fn test_parsing_ends_when_input_callback_returns_empty() {
271282
let mut parser = Parser::new();

lib/src/parser.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ static Subtree ts_parser__lex(
439439
}
440440

441441
if (self->lexer.current_position.bytes == error_end_position.bytes) {
442-
if (self->lexer.data.lookahead == 0) {
442+
if (self->lexer.data.eof(&self->lexer.data)) {
443443
self->lexer.data.result_symbol = ts_builtin_sym_error;
444444
break;
445445
}

lib/src/subtree.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -766,10 +766,10 @@ Subtree ts_subtree_last_external_token(Subtree tree) {
766766
}
767767

768768
static size_t ts_subtree__write_char_to_string(char *s, size_t n, int32_t c) {
769-
if (c == 0)
770-
return snprintf(s, n, "EOF");
771769
if (c == -1)
772770
return snprintf(s, n, "INVALID");
771+
else if (c == '\0')
772+
return snprintf(s, n, "'\\0'");
773773
else if (c == '\n')
774774
return snprintf(s, n, "'\\n'");
775775
else if (c == '\t')

test/fixtures/error_corpus/json_errors.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,4 @@ incomplete tokens at EOF
6565
nul
6666
---
6767

68-
(ERROR (UNEXPECTED EOF))
68+
(ERROR (UNEXPECTED '\0'))

0 commit comments

Comments
 (0)