From 057c6ad2ba018f94ca05b78f75e41d26ebb38050 Mon Sep 17 00:00:00 2001 From: "tree-sitter-ci-bot[bot]" <180118140+tree-sitter-ci-bot[bot]@users.noreply.github.com> Date: Mon, 2 Jun 2025 16:12:16 -0700 Subject: [PATCH] Fully fix field underflow in go_to_previous_sibling (#4483) (#4485) (cherry picked from commit 2ab9c9b59013efebd4631e9e5c47286ff03addd9) Co-authored-by: Max Brunsfeld Co-authored-by: Conrad Irwin --- cli/src/tests/tree_test.rs | 11 ++++++++++- lib/src/tree_cursor.c | 3 +-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/cli/src/tests/tree_test.rs b/cli/src/tests/tree_test.rs index a5dca965..a4941bb6 100644 --- a/cli/src/tests/tree_test.rs +++ b/cli/src/tests/tree_test.rs @@ -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"); } diff --git a/lib/src/tree_cursor.c b/lib/src/tree_cursor.c index 9d2ddd3d..561c1638 100644 --- a/lib/src/tree_cursor.c +++ b/lib/src/tree_cursor.c @@ -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];