1
Fork 0
mirror of git://git.sv.gnu.org/emacs.git synced 2026-01-07 04:10:27 -08:00

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.
This commit is contained in:
Yuan Fu 2023-04-13 14:36:46 -07:00
parent dff254946a
commit a5eb9f6ad4
No known key found for this signature in database
GPG key ID: 56E19BC57664A442

View file

@ -3247,6 +3247,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,
@ -3288,12 +3294,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;
}
@ -3345,12 +3357,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;
}
@ -3467,8 +3485,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)))