Fix infinite loop on unterminated string in query

This commit is contained in:
Max Brunsfeld 2019-09-11 14:44:49 -07:00
parent a1fec71b19
commit 86205b9e6d
2 changed files with 9 additions and 3 deletions

View file

@ -31,9 +31,10 @@ fn test_query_errors_on_invalid_syntax() {
Err(QueryError::Syntax(24))
);
// Return an error at the beginning of an unterminated string.
assert_eq!(
Query::new(language, "(if_statement condition:)"),
Err(QueryError::Syntax(24))
Query::new(language, r#"(identifier) "h "#),
Err(QueryError::Syntax(13))
);
});
}

View file

@ -398,7 +398,12 @@ static TSQueryError ts_query_parse_pattern(
// Parse the string content
const char *string_content = stream->input;
while (stream->next && stream->next != '"') stream_advance(stream);
while (stream->next != '"') {
if (!stream_advance(stream)) {
stream_reset(stream, string_content - 1);
return TSQueryErrorSyntax;
}
}
uint32_t length = stream->input - string_content;
// Add a step for the node