]> git.eshelyaron.com Git - emacs.git/commitdiff
Fix memory leak in ts_load_language
authorYuan Fu <casouri@gmail.com>
Mon, 17 Oct 2022 20:43:06 +0000 (13:43 -0700)
committerYuan Fu <casouri@gmail.com>
Mon, 17 Oct 2022 20:52:31 +0000 (13:52 -0700)
* src/treesit.c (ts_load_language): Move initialization of c_name down
and free it right after use.

src/treesit.c

index 8417b3bb1c529880bac182e7a5b21987aeddf377..90a1249ba8d5d9c5f2bed7ea623f36bd87698b80 100644 (file)
@@ -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)
     {