From 759cdf1e510d9e0689aefeced784fbb81644cd5e Mon Sep 17 00:00:00 2001 From: Yuan Fu Date: Thu, 13 Apr 2023 14:36:46 -0700 Subject: Catch signals produced by PRED in tree-sitter search functions Earlier we switched to using cursors rather than nodes to traverse the parse tree. Because cursors need cleanup, we have to catch signals thrown by the predicate functions and free the cursor. Failing to do this will result in leaking the cursor whenever the predicate function signals in a search function. This change fixes the leak. * src/treesit.c (treesit_traverse_cleanup_cursor): New function. (Ftreesit_search_subtree) (Ftreesit_search_forward) (Ftreesit_induce_sparse_tree): Catch signals. (Bug#62823) (cherry picked from commit a5eb9f6ad4e6f5a2819b540a477f1e889f6ef355) --- src/treesit.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/treesit.c b/src/treesit.c index 1bb52888f4b..7d5d033c02c 100644 --- a/src/treesit.c +++ b/src/treesit.c @@ -3156,6 +3156,12 @@ treesit_search_forward (TSTreeCursor *cursor, } } +/** Cleanup function for cursor. */ +static void treesit_traverse_cleanup_cursor(void *cursor) +{ + ts_tree_cursor_delete ((TSTreeCursor *) cursor); +} + DEFUN ("treesit-search-subtree", Ftreesit_search_subtree, Streesit_search_subtree, 2, 5, 0, @@ -3197,12 +3203,18 @@ Return the first matched node, or nil if none matches. */) if (!treesit_cursor_helper (&cursor, XTS_NODE (node)->node, parser)) return return_value; + specpdl_ref count = SPECPDL_INDEX (); + record_unwind_protect_ptr (treesit_traverse_cleanup_cursor, &cursor); + if (treesit_search_dfs (&cursor, predicate, parser, NILP (backward), NILP (all), the_limit, false)) { TSNode node = ts_tree_cursor_current_node (&cursor); return_value = make_treesit_node (parser, node); } + + unbind_to (count, Qnil); + ts_tree_cursor_delete (&cursor); return return_value; } @@ -3254,12 +3266,18 @@ always traverse leaf nodes first, then upwards. */) if (!treesit_cursor_helper (&cursor, XTS_NODE (start)->node, parser)) return return_value; + specpdl_ref count = SPECPDL_INDEX (); + record_unwind_protect_ptr (treesit_traverse_cleanup_cursor, &cursor); + if (treesit_search_forward (&cursor, predicate, parser, NILP (backward), NILP (all))) { TSNode node = ts_tree_cursor_current_node (&cursor); return_value = make_treesit_node (parser, node); } + + unbind_to (count, Qnil); + ts_tree_cursor_delete (&cursor); return return_value; } @@ -3376,8 +3394,14 @@ a regexp. */) to use treesit_cursor_helper. */ TSTreeCursor cursor = ts_tree_cursor_new (XTS_NODE (root)->node); + specpdl_ref count = SPECPDL_INDEX (); + record_unwind_protect_ptr (treesit_traverse_cleanup_cursor, &cursor); + treesit_build_sparse_tree (&cursor, parent, predicate, process_fn, the_limit, parser); + + unbind_to (count, Qnil); + ts_tree_cursor_delete (&cursor); Fsetcdr (parent, Fnreverse (Fcdr (parent))); if (NILP (Fcdr (parent))) -- cgit v1.2.3 From 14e809ddff1e60fb53d8570d9b5cfa3cb134858f Mon Sep 17 00:00:00 2001 From: Eli Zaretskii Date: Fri, 14 Apr 2023 09:21:05 +0300 Subject: Fix style and unwinding code in treesit.c This was supposed to be a cherry-pick from master, but isn't, due to the unnecessary rush to fix master without cleaning up the mess first and without separating changes that can and cannot be backported. * src/treesit.c (treesit_traverse_cleanup_cursor): Fix indentation style. (Ftreesit_search_subtree, Ftreesit_search_forward) (Ftreesit_induce_sparse_tree): Fix specpdl unwinding. (Bug#62823) Do not merge to master. --- src/treesit.c | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/src/treesit.c b/src/treesit.c index 7d5d033c02c..53dbeb68882 100644 --- a/src/treesit.c +++ b/src/treesit.c @@ -3156,8 +3156,9 @@ treesit_search_forward (TSTreeCursor *cursor, } } -/** Cleanup function for cursor. */ -static void treesit_traverse_cleanup_cursor(void *cursor) +/* Cleanup function for cursor. */ +static void +treesit_traverse_cleanup_cursor(void *cursor) { ts_tree_cursor_delete ((TSTreeCursor *) cursor); } @@ -3213,10 +3214,7 @@ Return the first matched node, or nil if none matches. */) return_value = make_treesit_node (parser, node); } - unbind_to (count, Qnil); - - ts_tree_cursor_delete (&cursor); - return return_value; + return unbind_to (count, return_value); } DEFUN ("treesit-search-forward", @@ -3276,10 +3274,7 @@ always traverse leaf nodes first, then upwards. */) return_value = make_treesit_node (parser, node); } - unbind_to (count, Qnil); - - ts_tree_cursor_delete (&cursor); - return return_value; + return unbind_to (count, return_value); } /* Recursively traverse the tree under CURSOR, and append the result @@ -3402,7 +3397,6 @@ a regexp. */) unbind_to (count, Qnil); - ts_tree_cursor_delete (&cursor); Fsetcdr (parent, Fnreverse (Fcdr (parent))); if (NILP (Fcdr (parent))) return Qnil; -- cgit v1.2.3