Subject: Re: bug#67977: 30.0.50; tree-sitter: Emacs crashes when accessing treesit-nodes in a narrowed buffer

From: Dmitry Gutov <[email-masked]>
Date: Sun, 31 Dec 2023 15:40:28 +0200

On 31/12/2023 12:39, Denis Zubarev wrote:
> Also there were errors when running benchmark in the narrowed buffer:
> Error muted by safe_call: (internal--syntax-propertize 1482) signaled 
> (args-out-of-range 1 1872)

That does look like a bug.

The patch below seems to fix it:

diff --git a/lisp/treesit.el b/lisp/treesit.el
index 264b95dc3a3..46ebadcf057 100644
--- a/lisp/treesit.el
+++ b/lisp/treesit.el
@@ -1150,7 +1150,7 @@ treesit--pre-syntax-ppss
      (if (and new-start (< new-start start))
          (progn
            (setq treesit--syntax-propertize-start nil)
-          (cons new-start end))
+          (cons (max new-start (point-min)) end))
        nil)))

  ;;; Indent

Or maybe syntax-propertize itself should have a protection against going 
outside of bounds:

diff --git a/lisp/emacs-lisp/syntax.el b/lisp/emacs-lisp/syntax.el
index e35992298a6..61a9e79b59c 100644
--- a/lisp/emacs-lisp/syntax.el
+++ b/lisp/emacs-lisp/syntax.el
@@ -431,7 +431,7 @@ syntax-propertize
                       (if (or (null new)
                               (and (>= (car new) start) (<= (cdr new) 
end)))
                           nil
-                       (setq start (car new))
+                       (setq start (max (car new) (point-min)))
                         (setq end (cdr new))
                         ;; If there's been a change, we should go 
through the
                         ;; list again since this new position may