mirror of
https://github.com/tree-sitter/tree-sitter.git
synced 2026-09-10 07:36:22 -04:00
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.
(cherry picked from commit dff828cdbe)
This commit is contained in:
parent
58edb3a11c
commit
81e7410b78
|
|
@ -330,6 +330,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()
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -2444,7 +2444,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| {
|
||||
|
|
|
|||
Loading…
Reference in a new issue