Fully fix field underflow in go_to_previous_sibling (#4483) (#4485)

(cherry picked from commit 2ab9c9b590)

Co-authored-by: Max Brunsfeld <maxbrunsfeld@gmail.com>
Co-authored-by: Conrad Irwin <conrad.irwin@gmail.com>
This commit is contained in:
tree-sitter-ci-bot[bot] 2025-06-02 16:12:16 -07:00 committed by GitHub
parent c44110c29f
commit 057c6ad2ba
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 11 additions and 3 deletions

View file

@ -388,7 +388,7 @@ fn test_tree_cursor_previous_sibling_with_aliases() {
.set_language(&get_test_fixture_language("aliases_in_root"))
.unwrap();
let text = "# comment\nfoo foo";
let text = "# comment\n# \nfoo foo";
let tree = parser.parse(text, None).unwrap();
let mut cursor = tree.walk();
assert_eq!(cursor.node().kind(), "document");
@ -396,12 +396,21 @@ fn test_tree_cursor_previous_sibling_with_aliases() {
cursor.goto_first_child();
assert_eq!(cursor.node().kind(), "comment");
assert!(cursor.goto_next_sibling());
assert_eq!(cursor.node().kind(), "comment");
assert!(cursor.goto_next_sibling());
assert_eq!(cursor.node().kind(), "bar");
assert!(cursor.goto_previous_sibling());
assert_eq!(cursor.node().kind(), "comment");
assert!(cursor.goto_previous_sibling());
assert_eq!(cursor.node().kind(), "comment");
assert!(cursor.goto_next_sibling());
assert_eq!(cursor.node().kind(), "comment");
assert!(cursor.goto_next_sibling());
assert_eq!(cursor.node().kind(), "bar");
}

View file

@ -135,12 +135,11 @@ static inline bool ts_tree_cursor_child_iterator_previous(
if (!extra && self->alias_sequence) {
*visible |= self->alias_sequence[self->structural_child_index];
if (self->child_index > 0) {
if (self->structural_child_index > 0) {
self->structural_child_index--;
}
}
// unsigned can underflow so compare it to child_count
if (self->child_index < self->parent.ptr->child_count) {
Subtree previous_child = ts_subtree_children(self->parent)[self->child_index];