From: Yuan Fu Date: Mon, 17 Oct 2022 20:43:06 +0000 (-0700) Subject: Fix memory leak in ts_load_language X-Git-Tag: emacs-29.0.90~1820 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=1cd42bfb8a5ff2aade43f31b864a8d2cd643d5a3;p=emacs.git Fix memory leak in ts_load_language * src/treesit.c (ts_load_language): Move initialization of c_name down and free it right after use. --- diff --git a/src/treesit.c b/src/treesit.c index 8417b3bb1c5..90a1249ba8d 100644 --- a/src/treesit.c +++ b/src/treesit.c @@ -497,9 +497,6 @@ treesit_load_language (Lisp_Object language_symbol, 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); /* Override the library name and C name, if appropriate. */ Lisp_Object override_name; @@ -508,10 +505,7 @@ treesit_load_language (Lisp_Object language_symbol, &override_name, &override_c_name); if (found_override) - { lib_base_name = override_name; - c_name = SSDATA (override_c_name); - } /* Now we generate a list of possible library paths. */ Lisp_Object path_candidates = Qnil; @@ -560,7 +554,12 @@ treesit_load_language (Lisp_Object language_symbol, /* Load TSLanguage. */ dynlib_error (); TSLanguage *(*langfn) (void); + char *c_name = strdup (SSDATA (base_name)); + treesit_symbol_to_c_name (c_name); + if (found_override) + c_name = SSDATA (override_c_name); langfn = dynlib_sym (handle, c_name); + free(c_name); error = dynlib_error (); if (error != NULL) {