From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Yuan Fu Newsgroups: gmane.emacs.bugs Subject: bug#62823: 29.0.60; Fixing a small leak in tree-sitter search functions Date: Thu, 13 Apr 2023 15:48:43 -0700 Message-ID: Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3731.500.231\)) Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="32584"; mail-complaints-to="usenet@ciao.gmane.io" To: 62823@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Fri Apr 14 00:50:17 2023 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pn5lh-0008It-Mp for geb-bug-gnu-emacs@m.gmane-mx.org; Fri, 14 Apr 2023 00:50:17 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pn5lT-0001e8-Uu; Thu, 13 Apr 2023 18:50:03 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pn5lT-0001e0-9E for bug-gnu-emacs@gnu.org; Thu, 13 Apr 2023 18:50:03 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pn5lT-0004gI-0G for bug-gnu-emacs@gnu.org; Thu, 13 Apr 2023 18:50:03 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1pn5lS-0005tH-CV for bug-gnu-emacs@gnu.org; Thu, 13 Apr 2023 18:50:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Yuan Fu Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Thu, 13 Apr 2023 22:50:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 62823 X-GNU-PR-Package: emacs X-Debbugs-Original-To: Bug Report Emacs Original-Received: via spool by submit@debbugs.gnu.org id=B.168142614422570 (code B ref -1); Thu, 13 Apr 2023 22:50:02 +0000 Original-Received: (at submit) by debbugs.gnu.org; 13 Apr 2023 22:49:04 +0000 Original-Received: from localhost ([127.0.0.1]:44997 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pn5kV-0005ry-O2 for submit@debbugs.gnu.org; Thu, 13 Apr 2023 18:49:04 -0400 Original-Received: from lists.gnu.org ([209.51.188.17]:50394) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pn5kR-0005rW-Vs for submit@debbugs.gnu.org; Thu, 13 Apr 2023 18:49:02 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pn5kR-0001ZF-HB for bug-gnu-emacs@gnu.org; Thu, 13 Apr 2023 18:48:59 -0400 Original-Received: from mail-pj1-x1033.google.com ([2607:f8b0:4864:20::1033]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pn5kP-0004Jx-HC for bug-gnu-emacs@gnu.org; Thu, 13 Apr 2023 18:48:59 -0400 Original-Received: by mail-pj1-x1033.google.com with SMTP id c10-20020a17090abf0a00b0023d1bbd9f9eso19905381pjs.0 for ; Thu, 13 Apr 2023 15:48:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1681426136; x=1684018136; h=to:date:message-id:subject:mime-version:content-transfer-encoding :from:from:to:cc:subject:date:message-id:reply-to; bh=nEgMbhPg+HzK/CqiV+cqrkNT3Kn6+5w4t//55uxNeIA=; b=AzrHHKc/VGkhi9oF31CtsulqX6sNfxtzV1hGwk04/If0XOx5QyMNdviBqtcBYmi4Gl VYDZJ0CeAKHi9Fq0HfkzOJ+/uIgUkaSKso/cphx8DgUxh6My2NrAe3vlC3Ed2P1bCL+4 FtNnf1olQgEmlwutL358QYm7o/aIt6r94HBTQbdpfuvwCSnUL+kLh3ZTPz5gPM2RSn87 UfSwgoBPsZGHhl6UANO251j1dXcTbh7axDZElL47m4o+LNccSLI3oYBnvdAcKjGKHVuv zERU73KbZW86FNPmBQQP2QQPd6Rt+QEV68v5ML20QBEYyeWWaJ87bFbWSyBcVYHOnO3l DvWw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681426136; x=1684018136; h=to:date:message-id:subject:mime-version:content-transfer-encoding :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=nEgMbhPg+HzK/CqiV+cqrkNT3Kn6+5w4t//55uxNeIA=; b=aVUVmEZ9IaXYbx9RcYMSVwGo+HgYXiWZib68oFEV9cvjrRWRoxZlk0CSpF1VLQVXDV jbwYKT/f0DZhWnjyXBixaorBH70bfCkM4YZHPJknQEhfIHrbkZ8qT8cXohUc8H6nNfl+ Bw3gGw3i0V10OQ5g0Y9v7YuPxyDabyWt0YSfJW7a9z3c9f9Jzl2NxSpz03PdML/wK/hO tZCZs06TO+ycVMUWpcya1h1aO9TYjaNqjYhwDIeDA2SgzNg/2uHUmklkaMFqqEV9+M+O Hp+VnNNTXIUo1cnpbynslNd9SCI99+8f3eeUGB7AVNUKt3b64uiIpHtHwByZd5Wdt4ct G8pQ== X-Gm-Message-State: AAQBX9dMboQ2PqygfRNuFSRafzhC2X8AjBvOnK+QLhJQ86h7Gp57nnG5 ScHjzGxktPaSOEkh7NDc59yH4jO5jbHZkQ== X-Google-Smtp-Source: AKy350Ype2p4qAJMxmZ0tW8N9BnaDh3G7jSQwK4VM1x5vBcDgQ/21VC5kbqBCvcZ7gISPJd6IIG3GA== X-Received: by 2002:a17:902:82c9:b0:1a5:19d1:f904 with SMTP id u9-20020a17090282c900b001a519d1f904mr492955plz.43.1681426135420; Thu, 13 Apr 2023 15:48:55 -0700 (PDT) Original-Received: from smtpclient.apple (cpe-172-117-161-177.socal.res.rr.com. [172.117.161.177]) by smtp.gmail.com with ESMTPSA id t20-20020a170902d21400b001a05122b562sm1920600ply.286.2023.04.13.15.48.54 for (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 13 Apr 2023 15:48:54 -0700 (PDT) X-Mailer: Apple Mail (2.3731.500.231) Received-SPF: pass client-ip=2607:f8b0:4864:20::1033; envelope-from=casouri@gmail.com; helo=mail-pj1-x1033.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:259881 Archived-At: When we switch to using cursors instead of nodes [1] in treesit.c for search functions, we introduced a small leak: the search functions allows the user to pass a predicate function, which can signal. And because we are now using cursors, which needs to be freed, everytime the predicate function signals, the cursor is leaked. I pushed a fix to master (a5eb9f6ad4e), the change is relatively straightforward so I hope we can pick it into the next pretest. And sorry for introducing the leak in the first place :-( Yuan [1] Commit e492c21e81040b9539139b78f6baf98df17bbaab (bug#60267) Below is the fix: commit a5eb9f6ad4e6f5a2819b540a477f1e889f6ef355 Author: Yuan Fu Date: Thu Apr 13 14:36:46 2023 -0700 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. diff --git a/src/treesit.c b/src/treesit.c index fd5fda78133..76d1dc8ccf4 100644 --- a/src/treesit.c +++ b/src/treesit.c @@ -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 @@ DEFUN ("treesit-search-subtree", 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 @@ DEFUN ("treesit-search-forward", 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 @@ DEFUN ("treesit-induce-sparse-tree", 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)))