Subject: 26.3; mhtml misbehaving font-lock extend region can cause Emacs to hang

From: Anders Lindgren <[email-masked]>
Date: Thu, 21 May 2020 22:14:00 +0200

Hi!

The font-lock system provides a system for major and minor modes to extend
the region that is being highlighted.

In Mhtml mode, the function `mhtml--extend-font-lock-region` sometimes
reduce the size of the region. If this is combined with, say, a minor mode
that extends the region then Emacs could hang.

Steps to repeat:

Create a file, test.html, with the following content:

---------
alpha

beta
---------

Evaluate:

(let ((font-lock-beg 7)
      (font-lock-end 13))
  (mhtml--extend-font-lock-region)
  (cons font-lock-beg font-lock-end))

The 7 and 13 corresponds to a region that includes the last two lines. The
expression returns (8 . 13) representing a region that only include the
last line. If this is combined with another function that extends the
region to include line 2, Emacs hangs.

I suggest that:

* Modify the documentation of the variable
`font-lock-extend-region-functions` so that it's clear that the region
should not shrink.

* Add a check in the function `font-lock-default-fontify-region` to prevent
the region from shrinking even if a function tries to do so. (By ensuring
that the region only extends and never shrinks, the function will always
terminate.)

* Fix the problem in `mhtml--extend-font-lock-region`.

One minor mode that, when enabled, would cause Emacs to hang with the above
buffer is https://github.com/Lindydancer/char-font-lock (this package
highlights incorrect whitespace).

    -- Anders Lindgren

1) The function `mhtml--extend-font-lock-region should be modified so that
it never shrinks the region.

In GNU Emacs 26.3 (build 1, x86_64-apple-darwin14.5.0, NS appkit-1348.17
Version 10.10.5 (Build 14F2511))
 of 2019-09-02 built on builder10-10.porkrind.org
Windowing system distributor 'Apple', version 10.3.1348
Recent messages:
For information about GNU Emacs and the GNU system, type C-h C-a.
(New file)
funcall-interactively: End of buffer
Saving file /Users/anders/emacs/src/foreach/foo.html...
Wrote /Users/anders/emacs/src/foreach/foo.html
Char: C-j (10, #o12, #xa) point=7 of 12 (50%) column=0
Quit
Entering debugger...
Back to top level
(8 . 13) [2 times]
Configured using:
 'configure --with-ns '--enable-locallisppath=/Library/Application
 Support/Emacs/${version}/site-lisp:/Library/Application
 Support/Emacs/site-lisp' --with-modules'

Configured features:
NOTIFY ACL GNUTLS LIBXML2 ZLIB TOOLKIT_SCROLL_BARS NS MODULES THREADS

Important settings:
  value of $LC_CTYPE: UTF-8
  value of $LANG: en_SE.UTF-8
  locale-coding-system: utf-8-unix

Major mode: HTML+

Minor modes in effect:
  tooltip-mode: t
  global-eldoc-mode: t
  electric-indent-mode: t
  mouse-wheel-mode: t
  tool-bar-mode: t
  menu-bar-mode: t
  file-name-shadow-mode: t
  global-font-lock-mode: t
  font-lock-mode: t
  blink-cursor-mode: t
  auto-composition-mode: t
  auto-encryption-mode: t
  auto-compression-mode: t
  line-number-mode: t
  transient-mark-mode: t

Load-path shadows:
None found.

Features:
(shadow sort mail-extr emacsbug message rmc puny dired dired-loaddefs
format-spec rfc822 mml mml-sec password-cache epa derived epg epg-config
gnus-util rmail rmail-loaddefs mm-decode mm-bodies mm-encode mail-parse
rfc2231 mailabbrev gmm-utils mailheader sendmail rfc2047 rfc2045
ietf-drums mm-util mail-prsvr mail-utils cl-print debug help-fns
radix-tree help-mode vc-dispatcher vc-svn mhtml-mode css-mode smie color
js advice json map imenu thingatpt cc-mode cc-fonts easymenu cc-guess
cc-menus cc-cmds cc-styles cc-align cc-engine cc-vars cc-defs sgml-mode
seq byte-opt gv bytecomp byte-compile cconv dom cl-loaddefs cl-lib
elec-pair time-date tooltip eldoc electric uniquify ediff-hook vc-hooks
lisp-float-type mwheel term/ns-win ns-win ucs-normalize mule-util
term/common-win tool-bar dnd fontset image regexp-opt fringe
tabulated-list replace newcomment text-mode elisp-mode lisp-mode
prog-mode register page menu-bar rfn-eshadow isearch timer select
scroll-bar mouse jit-lock font-lock syntax facemenu font-core
term/tty-colors frame cl-generic cham georgian utf-8-lang misc-lang
vietnamese tibetan thai tai-viet lao korean japanese eucjp-ms cp51932
hebrew greek romanian slovak czech european ethiopic indian cyrillic
chinese composite charscript charprop case-table epa-hook jka-cmpr-hook
help simple abbrev obarray minibuffer cl-preloaded nadvice loaddefs
button faces cus-face macroexp files text-properties overlay sha1 md5
base64 format env code-pages mule custom widget hashtable-print-readable
backquote threads kqueue cocoa ns multi-tty make-network-process emacs)

Memory information:
((conses 16 244001 21210)
 (symbols 48 23926 1)
 (miscs 40 81 268)
 (strings 32 43844 1478)
 (string-bytes 1 1234265)
 (vectors 16 39716)
 (vector-slots 8 775521 10394)
 (floats 8 209 245)
 (intervals 56 230 0)
 (buffers 992 14))