Subject: bug#61298: closed (Re: bug#61298: Emacs 29.0.60: font-lock throws an args-out-of-range error with a long line.)

From: [email-masked] (GNU bug Tracking System)
Date: Sun, 05 Feb 2023 16:05:01 +0000

Your bug report

#61298: Emacs 29.0.60: font-lock throws an args-out-of-range error with a long line.

which was filed against the emacs package, has been closed.

The explanation is attached below, along with your original report.
If you require more details, please reply to [email-masked].

-- 
61298: https://debbugs.gnu.org/cgi/bugreport.cgi?bug=61298
GNU Bug Tracking System
Contact [email-masked] with problems

Subject: Re: bug#61298: Emacs 29.0.60: font-lock throws an args-out-of-range error with a long line.
From: Alan Mackenzie <[email-masked]>
To: Eli Zaretskii <[email-masked]>
Cc: [email-masked]
Date: Sun, 5 Feb 2023 16:04:05 +0000
List-Id: <debbugs-submit.debbugs.gnu.org>

Hello, Eli.

On Sun, Feb 05, 2023 at 17:12:42 +0200, Eli Zaretskii wrote:
> > Date: Sun, 5 Feb 2023 14:54:35 +0000
> > From: Alan Mackenzie <[email-masked]>

> > Error: args-out-of-range (260028 260028)
> >   mapbacktrace(#f(compiled-function (evald func args flags) #<bytecode 0xdf8c26265ae87c7>))
> >   debug-early-backtrace()
> >   debug-early(error (args-out-of-range 260028 260028))  <===============
> >   get-text-property(260028 font-lock-multiline)
> >   font-lock-extend-region-multiline()
> >   font-lock-default-fontify-region(1 260029 nil)
> >   c-font-lock-fontify-region(1 1501 nil)
> >   font-lock-fontify-region(1 1501)
> >   #f(compiled-function (fun) #<bytecode -0x156eee0f20909c83>)(font-lock-fontify-region)
> >   run-hook-wrapped(#f(compiled-function (fun) #<bytecode -0x156eee0f20909c83>) font-lock-fontify-region)
> >   jit-lock--run-functions(1 1501)
> >   jit-lock-fontify-now(1 1501)
> >   jit-lock-function(1)
> >   redisplay_internal\ \(C\ function\)()

> > This is clearly a bug.

> > ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

> > Position 260028 is within the bounds of the buffer.  It would appear
> > that the new-ish long-lines code has narrowed the buffer at some stage
> > such that point-max is less than the end of the buffer.  I propose the
> > following simple patch which fixes (or works around) this bug:

> Fine with me, please install on the emacs-29 branch, and thanks.

I have done so, and I'm closing the bug with this post.

Thanks for such a rapid response.

-- 
Alan Mackenzie (Nuremberg, Germany).

Subject: Emacs 29.0.60: font-lock throws an args-out-of-range error with a long line.
From: Alan Mackenzie <[email-masked]>
To: [email-masked]
Date: Sun, 5 Feb 2023 14:54:35 +0000
List-Id: <debbugs-submit.debbugs.gnu.org>

Hello, Emacs.

This bug is reproducible in both the master branch and release branch as
updated yesterday evening (European time).

To reproduce this bug:
(i) Apply this bug fix to CC Mode, and byte compile the defun (or file),
and make sure it's loaded:

diff --git a/lisp/progmodes/cc-engine.el b/lisp/progmodes/cc-engine.el
index 2631c24f8db..9fa727d9e1f 100644
--- a/lisp/progmodes/cc-engine.el
+++ b/lisp/progmodes/cc-engine.el
@@ -5915,19 +5915,21 @@ c-determine-limit
 	(cond
 	 ((> pos start)			; Nothing but literals
 	  base)
-	 ((> base (point-min))
+	 ((and
+	   (> base (point-min))
+	   (> (- base try-size) (point-min))) ; prevent infinite recursion.
 	  (c-determine-limit how-far-back base (* 2 try-size) org-start))
 	 (t base)))
        ((>= count how-far-back)
 	(c-determine-limit-no-macro
-	(+ (car elt) (- count how-far-back))
-	org-start))
+	 (+ (car elt) (- count how-far-back))
+	 org-start))
        ((eq base (point-min))
 	(point-min))
        ((> base (- start try-size)) ; Can only happen if we hit point-min.
 	(c-determine-limit-no-macro
-	(car elt)
-	org-start))
+	 (car elt)
+	 org-start))
        (t
 	(c-determine-limit (- how-far-back count) base (* 2 try-size)
 			   org-start))))))

(ii) M-: (setq backtrace-on-redisplay-error t)
(iii) C-x b temp.cc.  M-x c++-mode.
(iv) Type in the following text:

char foo [] = R"foo(
  )foo"

(v) With point at the end of the first line in temp.cc,
  M-: (insert (make-string 260000 ?y))
  This may take some time to execute.
(vi) C-x 4 b *Redisplay_trace*
The following backtrace will be seen:

Error: args-out-of-range (260028 260028)
  mapbacktrace(#f(compiled-function (evald func args flags) #<bytecode 0xdf8c26265ae87c7>))
  debug-early-backtrace()
  debug-early(error (args-out-of-range 260028 260028))  <===============
  get-text-property(260028 font-lock-multiline)
  font-lock-extend-region-multiline()
  font-lock-default-fontify-region(1 260029 nil)
  c-font-lock-fontify-region(1 1501 nil)
  font-lock-fontify-region(1 1501)
  #f(compiled-function (fun) #<bytecode -0x156eee0f20909c83>)(font-lock-fontify-region)
  run-hook-wrapped(#f(compiled-function (fun) #<bytecode -0x156eee0f20909c83>) font-lock-fontify-region)
  jit-lock--run-functions(1 1501)
  jit-lock-fontify-now(1 1501)
  jit-lock-function(1)
  redisplay_internal\ \(C\ function\)()

This is clearly a bug.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Position 260028 is within the bounds of the buffer.  It would appear
that the new-ish long-lines code has narrowed the buffer at some stage
such that point-max is less than the end of the buffer.  I propose the
following simple patch which fixes (or works around) this bug:

diff --git a/lisp/font-lock.el b/lisp/font-lock.el
index 1fa45379b9f..9e944fe188a 100644
--- a/lisp/font-lock.el
+++ b/lisp/font-lock.el
@@ -1154,6 +1154,8 @@ font-lock-default-fontify-region
   "Fontify the text between BEG and END.
 If LOUDLY is non-nil, print status messages while fontifying.
 This function is the default `font-lock-fontify-region-function'."
+  (or (<= end (point-max))
+      (setq end (point-max)))
   (with-silent-modifications
    ;; Use the fontification syntax table, if any.
    (with-syntax-table (or font-lock-syntax-table (syntax-table))

-- 
Alan Mackenzie (Nuremberg, Germany).