From 1cd42bfb8a5ff2aade43f31b864a8d2cd643d5a3 Mon Sep 17 00:00:00 2001 From: Yuan Fu Date: Mon, 17 Oct 2022 13:43:06 -0700 Subject: [PATCH] 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. --- src/treesit.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) 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) { -- 2.39.2