]> git.eshelyaron.com Git - emacs.git/commitdiff
Fix treesit-parse-string crash (bug#71012)
authorYuan Fu <casouri@gmail.com>
Sat, 1 Jun 2024 17:33:18 +0000 (10:33 -0700)
committerEshel Yaron <me@eshelyaron.com>
Sat, 8 Jun 2024 12:58:43 +0000 (14:58 +0200)
Parsing a large file with treesit-parse-string and then printing the
returned node crashes Emacs, because with-temp-buffer kills the temp
buffer when treesit-parse-string returns, and print.c tries to access
the node's position in the killed buffer.

* lisp/treesit.el (treesit-parse-string): Don't use with-temp-buffer.

(cherry picked from commit 00360258caddc0d8cf29ba3d9971125a06f8959b)

lisp/treesit.el

index bcabf98f1d1832f41fdbf28453203cdac3e43d2b..ee67c2349f9fc5296d760001c180d98b3589ae24 100644 (file)
@@ -126,10 +126,13 @@ of max unsigned 32-bit value for byte offsets into buffer text."
 (defun treesit-parse-string (string language)
   "Parse STRING using a parser for LANGUAGE.
 Return the root node of the syntax tree."
-  (with-temp-buffer
-    (insert string)
-    (treesit-parser-root-node
-     (treesit-parser-create language))))
+  ;; We can't use `with-temp-buffer' because it kills the buffer when
+  ;; returning from the form.
+  (let ((buf (generate-new-buffer " *treesit-parse-string*")))
+    (with-current-buffer buf
+      (insert string)
+      (treesit-parser-root-node
+       (treesit-parser-create language)))))
 
 (defvar-local treesit-language-at-point-function nil
   "A function that returns the language at point.