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.
This commit is contained in:
Riley Bruins 2025-07-21 21:44:21 -07:00 committed by Christian Clason
parent c18d019db0
commit dff828cdbe
2 changed files with 11 additions and 1 deletions

View file

@ -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()
}
);
});
}

View file

@ -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| {