From dff828cdbe5e21554d33b16160bab9878879e458 Mon Sep 17 00:00:00 2001 From: Riley Bruins Date: Mon, 21 Jul 2025 21:44:21 -0700 Subject: [PATCH] fix(rust): prevent overflow in error message calculation **Problem:** When encountering an invalid symbol at the beginning of the file, the rust bindings attempt to index the character at position -1 of the query source, which leads to an overflow and thus invalid character index which causes a panic. **Solution:** Bounds check the offset before performing the subtraction. --- crates/cli/src/tests/query_test.rs | 10 ++++++++++ lib/binding_rust/lib.rs | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/crates/cli/src/tests/query_test.rs b/crates/cli/src/tests/query_test.rs index 5486a537..7c436ede 100644 --- a/crates/cli/src/tests/query_test.rs +++ b/crates/cli/src/tests/query_test.rs @@ -342,6 +342,16 @@ fn test_query_errors_on_invalid_symbols() { message: "\"alternatives\"".to_string() } ); + assert_eq!( + Query::new(&language, "fakefield: (identifier)").unwrap_err(), + QueryError { + row: 0, + offset: 0, + column: 0, + kind: QueryErrorKind::Field, + message: "\"fakefield\"".to_string() + } + ); }); } diff --git a/lib/binding_rust/lib.rs b/lib/binding_rust/lib.rs index 3ffd90c4..a4cea362 100644 --- a/lib/binding_rust/lib.rs +++ b/lib/binding_rust/lib.rs @@ -2446,7 +2446,7 @@ impl Query { // Error types that report names ffi::TSQueryErrorNodeType | ffi::TSQueryErrorField | ffi::TSQueryErrorCapture => { let suffix = source.split_at(offset).1; - let in_quotes = source.as_bytes()[offset - 1] == b'"'; + let in_quotes = offset > 0 && source.as_bytes()[offset - 1] == b'"'; let mut backslashes = 0; let end_offset = suffix .find(|c| {