fix(query): correct last_child_step_index in cases where a new step

wasn't created.

This fixes an OOB access to `self.steps` when a last child anchor
immediately follows a predicate.
This commit is contained in:
Will Lillis 2025-05-01 23:46:40 -04:00
parent cc634236b1
commit b1d2b7cfb8
2 changed files with 15 additions and 1 deletions

View file

@ -5621,3 +5621,14 @@ const foo = [
assert_eq!(matches.len(), 1);
assert_eq!(matches[0].1, captures);
}
#[test]
fn test_query_with_predicate_causing_oob_access() {
let language = get_language("rust");
let query = "(call_expression
function: (scoped_identifier
path: (scoped_identifier (identifier) @_regex (#any-of? @_regex \"Regex\" \"RegexBuilder\") .))
(#set! injection.language \"regex\"))";
Query::new(&language, query).unwrap();
}

View file

@ -2523,6 +2523,9 @@ static TSQueryError ts_query__parse_pattern(
child_is_immediate,
&child_capture_quantifiers
);
// In the event we only parsed a predicate, meaning no new steps were added,
// then subtract one so we're not indexing past the end of the array
if (step_index == self->steps.size) step_index--;
if (e == PARENT_DONE) {
if (stream->next == ')') {
if (child_is_immediate) {
@ -2531,7 +2534,7 @@ static TSQueryError ts_query__parse_pattern(
return TSQueryErrorSyntax;
}
// Mark this step *and* its alternatives as the last child of the parent.
QueryStep *last_child_step = &self->steps.contents[last_child_step_index];
QueryStep *last_child_step = array_get(&self->steps, last_child_step_index);
last_child_step->is_last_child = true;
if (
last_child_step->alternative_index != NONE &&