unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Yuan Fu <casouri@gmail.com>
To: "Daniel Martín" <mardani29@yahoo.es>
Cc: 58596@debbugs.gnu.org
Subject: bug#58596: 29.0.50; [PATCH] Fix memory leak when loading Tree-sitter language definitions
Date: Thu, 27 Oct 2022 16:39:10 -0700	[thread overview]
Message-ID: <95784A3C-B2F1-415E-94B2-F08B2837AF31@gmail.com> (raw)
In-Reply-To: <m1sfjmm56m.fsf@yahoo.es>


Daniel Martín <mardani29@yahoo.es> writes:

> I'm instrumenting the Tree-sitter branch to make sure there's no glaring
> memory issues like leaks or undefined behavior.  I've found that the
> function treesit_load_language leaks a few bytes each time a language is
> loaded.
>
> To fix the bug, I've simplified a bit the logic that loads the dynamic
> library, to avoid the string duplication that was leaking, and removed a
> loop that I think it's not really necessary (that'll save us a few CPU
> cycles).
>
> Please check that I've not made any crucial mistake, and feel free to
> merge it if you think it's a good patch.
>
> Thanks.
>

Sorry! I just see this.  I believe I fixed the memory leak in trunk.
The purpose for treesit_symbol_to_c_name is to transform not dashes in
tree-sitter, but dashes in languages names, eg, c-sharp.  So we do need
that.  Thank you so much on working on this though!

If you don’t mind, please have a look at the revised code and see if it
fixes the memory leak.

Yuan

>>From fbe2b320e4e41cd8a522bb4b56c47e6509d26d08 Mon Sep 17 00:00:00 2001
> From: =?UTF-8?q?Daniel=20Mart=C3=ADn?= <mardani29@yahoo.es>
> Date: Tue, 18 Oct 2022 00:41:15 +0200
> Subject: [PATCH] Fix memory leak when loading Tree-sitter language definitions
>
> * src/treesit.c (treesit_load_language): Simplify and avoid strdup
> call.
> (treesit_symbol_to_c_name): Remove now unused function.
> ---
>  src/treesit.c | 21 +++------------------
>  1 file changed, 3 insertions(+), 18 deletions(-)
>
> diff --git a/src/treesit.c b/src/treesit.c
> index 8417b3bb1c..1e0694f84b 100644
> --- a/src/treesit.c
> +++ b/src/treesit.c
> @@ -429,18 +429,6 @@ treesit_initialize (void)
>  \f
>  /*** Loading language library */
>  
> -/* Translates a symbol treesit-<lang> to a C name
> -   treesit_<lang>.  */
> -static void
> -treesit_symbol_to_c_name (char *symbol_name)
> -{
> -  for (int idx = 0; idx < strlen (symbol_name); idx++)
> -    {
> -      if (symbol_name[idx] == '-')
> -	symbol_name[idx] = '_';
> -    }
> -}
> -
>  static bool
>  treesit_find_override_name (Lisp_Object language_symbol, Lisp_Object *name,
>  			    Lisp_Object *c_symbol)
> @@ -496,10 +484,7 @@ treesit_load_language (Lisp_Object language_symbol,
>    Lisp_Object lib_base_name =
>      concat2 (build_pure_c_string ("libtree-sitter-"), symbol_name);
>    Lisp_Object base_name =
> -    concat2 (build_pure_c_string ("tree-sitter-"), symbol_name);
> -  /* FIXME: The result of strdup leaks memory in some cases.  */
> -  char *c_name = strdup (SSDATA (base_name));
> -  treesit_symbol_to_c_name (c_name);
> +    concat2 (build_pure_c_string ("tree_sitter_"), symbol_name);
>  
>    /* Override the library name and C name, if appropriate.  */
>    Lisp_Object override_name;
> @@ -510,7 +495,7 @@ treesit_load_language (Lisp_Object language_symbol,
>    if (found_override)
>      {
>        lib_base_name = override_name;
> -      c_name = SSDATA (override_c_name);
> +      base_name = override_c_name;
>      }
>  
>    /* Now we generate a list of possible library paths.  */
> @@ -560,7 +545,7 @@ treesit_load_language (Lisp_Object language_symbol,
>    /* Load TSLanguage.  */
>    dynlib_error ();
>    TSLanguage *(*langfn) (void);
> -  langfn = dynlib_sym (handle, c_name);
> +  langfn = dynlib_sym (handle, SSDATA (base_name));
>    error = dynlib_error ();
>    if (error != NULL)
>      {





  reply	other threads:[~2022-10-27 23:39 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <m1sfjmm56m.fsf.ref@yahoo.es>
2022-10-17 22:54 ` bug#58596: 29.0.50; [PATCH] Fix memory leak when loading Tree-sitter language definitions Daniel Martín via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-10-27 23:39   ` Yuan Fu [this message]
2022-10-28 19:49     ` Daniel Martín via Bug reports for GNU Emacs, the Swiss army knife of text editors

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=95784A3C-B2F1-415E-94B2-F08B2837AF31@gmail.com \
    --to=casouri@gmail.com \
    --cc=58596@debbugs.gnu.org \
    --cc=mardani29@yahoo.es \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).