2

Emacs 24.x and 25.x have a bug in CC Mode which can result in some operations failing with the error

Invalid search bound (wrong side of point)

For example, I can reproduce it with the following file a.c:

#if defined(FOO) || \
    defined(BAR)

and running

emacs -Q -nw a.c --eval '(occur "FOO")'

(Running M-x occur RET FOO RET after that works. I have a bigger file where this doesn't work.)

This is a known bug: #28850. The fix is in two commits 46540a1c7adb1b89b6c2f6c9150fe8680c3a5fba and ad68bbd0da4ed90117f09dc2344c0c3d9d728851 which were first released in Emacs 26.1. I can confirm that Emacs 26.3 works with both the minimal example above and my real file.

The machines I use most often run Emacs 24.5.1 or Emacs 25.2.2, so I need a workaround for Emacs 24.x and 25.x. My init file has several hot fixes for Emacs bug and I'm fine with adding one more. I normally hot-fix in an eval-after-load form, either by advising a function or by replacing its definition.

But here there's a bug in the function c-make-font-lock-search-form which, if I understand correctly, is used at compile time. This is the relevant part of cc-fonts.el:

(eval-and-compile
  …
  (defun c-make-font-lock-search-form (regexp highlights)
    ;; this code needs to be patched
    …)
  (defun c-make-font-lock-context-search-function (normal &rest state-stanzas)
    (c-make-font-lock-search-form …)) ;; this call needs to be patched
)

(c-lang-defconst c-cpp-matchers
  t `( … ,(c-make-font-lock-context-search-function …)))

So cc-fonts.elc contains buggy byte-compiled code. Merely fixing the buggy function after cc-fonts.elc is loaded won't help. Even fixing the function before cc-fonts.elc wouldn't help.

How can I work around this bug? The workaround needs to work on Emacs 24.x and 25.x. The workaround needs to fix the "Invalid search bound" error, without disabling fontification. I don't mind if there's a slight loss of performance or accuracy compared to the official fix.

Drew
  • 75,699
  • 9
  • 109
  • 225
  • Scanning the bug report, does it help to turn off `jit-lock-stealth`? – Drew Oct 28 '19 at 20:12
  • @Drew Isn't it off by default? `jit-lock-stealth-time` defaults to nil, it's nil in my configuration, and its documentation states “If nil, stealth fontification is never performed.”. I don't do anything special regarding `jit-lock-mode`, so it's turned on with default parameters. I think Jit Lock is just the way the reporter happened to trigger the bug. With me the trigger was `occur`. – Gilles 'SO- stop being evil' Oct 29 '19 at 18:55
  • Dunno. The bug report seemed to be about the problem happening when it's non-`nil`. – Drew Oct 29 '19 at 20:47

1 Answers1

1

A big-hammer approach is to add cc-mode to your config, as it is maintained externally and tends to be very backwards-compatible.

The latest version says "CC Mode 5.34 should work out of the box with Emacs >= 24.5 ... It may well work on earlier versions."

The 5.33 release notes say "The minimum versions supported ... Emacs 23.2".

phils
  • 48,657
  • 3
  • 76
  • 115