From 86205b9e6d2ceb2759f1e67e4e474cf73bb4a395 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Wed, 11 Sep 2019 14:44:49 -0700 Subject: [PATCH] Fix infinite loop on unterminated string in query --- cli/src/tests/query_test.rs | 5 +++-- lib/src/query.c | 7 ++++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/cli/src/tests/query_test.rs b/cli/src/tests/query_test.rs index 5c37a9ab..2ac178fc 100644 --- a/cli/src/tests/query_test.rs +++ b/cli/src/tests/query_test.rs @@ -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)) ); }); } diff --git a/lib/src/query.c b/lib/src/query.c index ea01cf24..10d409ed 100644 --- a/lib/src/query.c +++ b/lib/src/query.c @@ -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