unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* Re: emacs-29 c9ba05af8d: Fix crashes inside 'xfree' called from treesit.c
       [not found] ` <20230207195405.BB290C00231@vcs2.savannah.gnu.org>
@ 2023-02-08 15:43   ` Eric Gillespie
  2023-02-08 16:42     ` Eli Zaretskii
  0 siblings, 1 reply; 2+ messages in thread
From: Eric Gillespie @ 2023-02-08 15:43 UTC (permalink / raw)
  To: emacs-devel, Eli Zaretskii

Eli Zaretskii <eliz@gnu.org> writes:

> commit c9ba05af8dfabca00023bd2312dec4ec59497801
> diff --git a/src/treesit.c b/src/treesit.c
> index 8e772523cc..b15d44fca0 100644
> --- a/src/treesit.c
> +++ b/src/treesit.c
> @@ -620,7 +620,7 @@ treesit_load_language (Lisp_Object language_symbol,
>    char *c_name = xstrdup (SSDATA (base_name));
>    treesit_symbol_to_c_name (c_name);
>    if (found_override)
> -    c_name = SSDATA (override_c_name);
> +    c_name = xstrdup (SSDATA (override_c_name));
>    langfn = dynlib_sym (handle, c_name);
>    xfree (c_name);
>    error = dynlib_error ();

Isn't the memory allocated on line 620 (in the initial c_name
assignment) leaked when found_override is true?

It looks like this dates to commit
1cd42bfb8a5ff2aade43f31b864a8d2cd643d5a3, which, ironically, was
fixing another memory leak.  It looks like this code has been
suffering a lot of memory issues, actually.

Maybe I'm misunderstanding, but it looks to me like not only is
the memory initially pointed to by c_name leaked, but that that
allocation is unnecessary when found_override is true, along with
the work done by treesit_symbol_to_c_name.

Is this wrong?

Thanks!

From 75b5f2822d547c4e3926571661ab7fb4ea4d1317 Mon Sep 17 00:00:00 2001
From: Eric Gillespie <epg@pretzelnet.org>
Date: Wed, 8 Feb 2023 09:41:20 -0600
Subject: [PATCH] Fix another memory leak in treesit.c

* src/treesit.c (treesit_load_language): Always xstrdup 'c_name',
always xfree it, and let it go out of scope after free.
---
 src/treesit.c | 21 +++++++++++++++------
 1 file changed, 15 insertions(+), 6 deletions(-)

diff --git a/src/treesit.c b/src/treesit.c
index b15d44fca01..de8a0962f3b 100644
--- a/src/treesit.c
+++ b/src/treesit.c
@@ -617,12 +617,21 @@ treesit_load_language (Lisp_Object language_symbol,
   eassume (handle != NULL);
   dynlib_error ();
   TSLanguage *(*langfn) (void);
-  char *c_name = xstrdup (SSDATA (base_name));
-  treesit_symbol_to_c_name (c_name);
-  if (found_override)
-    c_name = xstrdup (SSDATA (override_c_name));
-  langfn = dynlib_sym (handle, c_name);
-  xfree (c_name);
+  {
+    /* c_name must be freed in either case. */
+    char *c_name;
+    if (found_override)
+      {
+	c_name = xstrdup (SSDATA (override_c_name));
+      }
+    else
+      {
+	c_name = xstrdup (SSDATA (base_name));
+	treesit_symbol_to_c_name (c_name);
+      }
+    langfn = dynlib_sym (handle, c_name);
+    xfree (c_name);
+  }
   error = dynlib_error ();
   if (error != NULL)
     {
-- 
2.35.3




^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: emacs-29 c9ba05af8d: Fix crashes inside 'xfree' called from treesit.c
  2023-02-08 15:43   ` emacs-29 c9ba05af8d: Fix crashes inside 'xfree' called from treesit.c Eric Gillespie
@ 2023-02-08 16:42     ` Eli Zaretskii
  0 siblings, 0 replies; 2+ messages in thread
From: Eli Zaretskii @ 2023-02-08 16:42 UTC (permalink / raw)
  To: Eric Gillespie; +Cc: emacs-devel

> From: Eric Gillespie <epg@pretzelnet.org>
> Date: Wed, 08 Feb 2023 09:43:51 -0600
> 
> Eli Zaretskii <eliz@gnu.org> writes:
> 
> > commit c9ba05af8dfabca00023bd2312dec4ec59497801
> > diff --git a/src/treesit.c b/src/treesit.c
> > index 8e772523cc..b15d44fca0 100644
> > --- a/src/treesit.c
> > +++ b/src/treesit.c
> > @@ -620,7 +620,7 @@ treesit_load_language (Lisp_Object language_symbol,
> >    char *c_name = xstrdup (SSDATA (base_name));
> >    treesit_symbol_to_c_name (c_name);
> >    if (found_override)
> > -    c_name = SSDATA (override_c_name);
> > +    c_name = xstrdup (SSDATA (override_c_name));
> >    langfn = dynlib_sym (handle, c_name);
> >    xfree (c_name);
> >    error = dynlib_error ();
> 
> Isn't the memory allocated on line 620 (in the initial c_name
> assignment) leaked when found_override is true?
> 
> It looks like this dates to commit
> 1cd42bfb8a5ff2aade43f31b864a8d2cd643d5a3, which, ironically, was
> fixing another memory leak.  It looks like this code has been
> suffering a lot of memory issues, actually.
> 
> Maybe I'm misunderstanding, but it looks to me like not only is
> the memory initially pointed to by c_name leaked, but that that
> allocation is unnecessary when found_override is true, along with
> the work done by treesit_symbol_to_c_name.
> 
> Is this wrong?

Thanks, you are right.  I fixed this (hopefully) using almost the same
code as you suggested.



^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-02-08 16:42 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <167579964545.12597.15873702837915034838@vcs2.savannah.gnu.org>
     [not found] ` <20230207195405.BB290C00231@vcs2.savannah.gnu.org>
2023-02-08 15:43   ` emacs-29 c9ba05af8d: Fix crashes inside 'xfree' called from treesit.c Eric Gillespie
2023-02-08 16:42     ` Eli Zaretskii

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).