Subject: Re: bug#28850: 26.0.90; Error running timer 'jit-lock-stealth-fontify': (error "Invalid search bound (wrong side of point)")

From: Eli Zaretskii <[email-masked]>
Date: Wed, 01 May 2019 21:49:07 +0300

> Date: Tue, 30 Apr 2019 18:26:26 +0300
> From: Eli Zaretskii <[email-masked]>
> Cc: [email-masked], [email-masked]
> 
> FWIW, I think I see something similar in Emacs 26.2, I will try to
> catch it one of these days.

Done.  The error messages are slightly different: wrong-type-argument
number-or-marker-p nil.

It was very hard to debug, but eventually I succeeded to catch a
backtrace:

  Thread 1 hit Breakpoint 5, wrong_type_argument (predicate=XIL(0x9f90),
      value=XIL(0)) at data.c:146
  146     {
  #0  wrong_type_argument (predicate=XIL(0x9f90), value=XIL(0)) at data.c:146
  #1  0x01127519 in CHECK_TYPE (x=XIL(0), predicate=<optimized out>, ok=0)
      at lisp.h:625
  #2  Fadd1 (number=XIL(0)) at data.c:3125
  #3  0x011429ba in eval_sub (form=XIL(0xc000000018bb6b60)) at eval.c:2241
  #4  0x01142768 in eval_sub (form=XIL(0xc000000018bb6b40)) at eval.c:2229
  #5  0x01142ee0 in Fprogn (body=<optimized out>) at eval.c:459
  #6  0x0113af02 in Fsave_excursion (args=XIL(0xc000000018bb6af0))
      at editfns.c:1050
  #7  0x01142a9a in eval_sub (form=XIL(0xc000000018bb6b20)) at eval.c:2193
  #8  0x0114300b in Fprogn (body=<optimized out>) at eval.c:459
  #9  Fcond (args=<optimized out>) at eval.c:439
  #10 0x01142a9a in eval_sub (form=XIL(0xc000000018bb6950)) at eval.c:2193
  #11 0x01146b24 in Fsetq (args=XIL(0xc000000018bb6930)) at eval.c:517
  #12 0x01142a9a in eval_sub (form=XIL(0xc000000018bb6940)) at eval.c:2193
  #13 0x01147612 in Fprogn (body=<optimized out>) at eval.c:459
  #14 Flet (args=<optimized out>) at eval.c:973
  #15 0x01142a9a in eval_sub (form=XIL(0xc000000018bb66d0)) at eval.c:2193
  #16 0x01142f53 in Fprogn (body=<optimized out>) at eval.c:459
  #17 Fif (args=XIL(0xc000000018b9ff50)) at eval.c:415
  #18 0x01142a9a in eval_sub (form=XIL(0xc000000018b9ffa0)) at eval.c:2193
  #19 0x01142ee0 in Fprogn (body=<optimized out>) at eval.c:459
  #20 0x01141574 in internal_catch (tag=XIL(0x809f040),
      func=func@entry=0x1142eb2 <Fprogn>, arg=XIL(0xc000000018ba0490))
      at eval.c:1101
  #21 0x0114782a in Fcatch (args=XIL(0xc000000018ba04e0)) at eval.c:1078
  #22 0x01142a9a in eval_sub (form=XIL(0xc000000018ba04f0)) at eval.c:2193
  #23 0x011477e1 in Fwhile (args=XIL(0xc000000018b1c810)) at eval.c:989
  #24 0x01142a9a in eval_sub (form=XIL(0xc000000018b1c840)) at eval.c:2193
  #25 0x01142f53 in Fprogn (body=<optimized out>) at eval.c:459
  #26 Fif (args=XIL(0xc000000018b7a4b0)) at eval.c:415
  #27 0x01142a9a in eval_sub (form=XIL(0xc000000018b7a4c0)) at eval.c:2193
  #28 0x01142ee0 in Fprogn (body=<optimized out>) at eval.c:459
  #29 0x0113dc1a in Fsave_restriction (body=XIL(0xc000000018bb5160))
      at editfns.c:3990
  #30 0x01142a9a in eval_sub (form=XIL(0xc000000018bb5170)) at eval.c:2193
  #31 0x01147612 in Fprogn (body=<optimized out>) at eval.c:459
  #32 Flet (args=<optimized out>) at eval.c:973
  #33 0x01142a9a in eval_sub (form=XIL(0xc000000018625d50)) at eval.c:2193
  #34 0x0114345d in Fprogn (body=<optimized out>) at eval.c:459
  #35 funcall_lambda (fun=XIL(0xc000000018625d40), nargs=nargs@entry=3,
      arg_vector=arg_vector@entry=0x88bbf0) at eval.c:3052
  #36 0x0114215a in apply_lambda (fun=<optimized out>, args=<optimized out>,
      count=<optimized out>, count@entry=84) at eval.c:2913
  #37 0x0114263a in eval_sub (form=XIL(0xc00000001c7bd2b0)) at eval.c:2316
  #38 0x01146b24 in Fsetq (args=XIL(0xc00000001c7bd2c0)) at eval.c:517
  #39 0x01142a9a in eval_sub (form=XIL(0xc00000001c7bd2d0)) at eval.c:2193
  #40 0x01142768 in eval_sub (form=XIL(0xc00000001c7bd2e0)) at eval.c:2229
  #41 0x01142768 in eval_sub (form=XIL(0xc00000001c7bd2f0)) at eval.c:2229
  #42 0x01142e9e in Fand (args=<optimized out>) at eval.c:393
  #43 0x01142a9a in eval_sub (form=XIL(0xc00000001c7c5550)) at eval.c:2193
  #44 0x011477e1 in Fwhile (args=XIL(0xc00000001c7c5380)) at eval.c:989
  #45 0x01142a9a in eval_sub (form=XIL(0xc00000001c7c5390)) at eval.c:2193
  #46 0x01147612 in Fprogn (body=<optimized out>) at eval.c:459
  #47 Flet (args=<optimized out>) at eval.c:973
  #48 0x01142a9a in eval_sub (form=XIL(0xc00000001c7c52c0)) at eval.c:2193
  #49 0x01147233 in Fprogn (body=<optimized out>) at eval.c:459
  #50 FletX (args=XIL(0xc00000001c7c3ed0)) at eval.c:904
  #51 0x01142a9a in eval_sub (form=XIL(0xc00000001c7c3ec0)) at eval.c:2193
  #52 0x01142ee0 in Fprogn (body=<optimized out>) at eval.c:459
  #53 0x01141574 in internal_catch (tag=XIL(0xfeb19468),
      func=func@entry=0x1142eb2 <Fprogn>, arg=XIL(0xc00000001c7c3e90))
      at eval.c:1101
  #54 0x0114782a in Fcatch (args=XIL(0xc00000001c7c3ea0)) at eval.c:1078
  #55 0x01142a9a in eval_sub (form=XIL(0xc00000001c7c3eb0)) at eval.c:2193
  #56 0x0114345d in Fprogn (body=<optimized out>) at eval.c:459
  #57 funcall_lambda (fun=XIL(0xc00000001c7c3e80), nargs=nargs@entry=1,
      arg_vector=arg_vector@entry=0x88c810) at eval.c:3052
  #58 0x011437c9 in Ffuncall (nargs=2, args=args@entry=0x88c808) at eval.c:2790
  #59 0x01189680 in exec_byte_code (bytestr=<optimized out>,
      vector=<optimized out>, maxdepth=<optimized out>,
      args_template=<optimized out>, nargs=<optimized out>, nargs@entry=0,
      args=<optimized out>, args@entry=0x0) at bytecode.c:630
  #60 0x01143291 in funcall_lambda (fun=XIL(0xa00000000adfab48),
      nargs=nargs@entry=1, arg_vector=arg_vector@entry=0x88cb00) at eval.c:3059
  #61 0x011437c9 in Ffuncall (nargs=2, args=args@entry=0x88caf8) at eval.c:2790
  #62 0x01189680 in exec_byte_code (bytestr=<optimized out>,
      vector=<optimized out>, maxdepth=<optimized out>,
      args_template=<optimized out>, nargs=<optimized out>, nargs@entry=0,
      args=<optimized out>, args@entry=0x0) at bytecode.c:630
  #63 0x01143291 in funcall_lambda (fun=XIL(0xa000000001324700),
      nargs=nargs@entry=3, arg_vector=arg_vector@entry=0x88d0c0) at eval.c:3059
  #64 0x011437c9 in Ffuncall (nargs=4, args=args@entry=0x88d0b8) at eval.c:2790
  #65 0x01189680 in exec_byte_code (bytestr=<optimized out>,
      vector=<optimized out>, maxdepth=<optimized out>,
      args_template=<optimized out>, nargs=<optimized out>, nargs@entry=0,
      args=<optimized out>, args@entry=0x0) at bytecode.c:630
  #66 0x01143291 in funcall_lambda (fun=XIL(0xa000000001323638),
      nargs=nargs@entry=3, arg_vector=arg_vector@entry=0x88d410) at eval.c:3059
  #67 0x011437c9 in Ffuncall (nargs=4, args=args@entry=0x88d408) at eval.c:2790
  #68 0x01189680 in exec_byte_code (bytestr=<optimized out>,
      vector=<optimized out>, maxdepth=<optimized out>,
      args_template=<optimized out>, nargs=<optimized out>, nargs@entry=0,
      args=<optimized out>, args@entry=0x0) at bytecode.c:630
  #69 0x01143291 in funcall_lambda (fun=XIL(0xa00000000ae74d20),
      nargs=nargs@entry=3, arg_vector=arg_vector@entry=0x88d740) at eval.c:3059
  #70 0x011437c9 in Ffuncall (nargs=4, args=args@entry=0x88d738) at eval.c:2790
  #71 0x01189680 in exec_byte_code (bytestr=<optimized out>,
      vector=<optimized out>, maxdepth=<optimized out>,
      args_template=<optimized out>, nargs=<optimized out>, nargs@entry=0,
      args=<optimized out>, args@entry=0x0) at bytecode.c:630
  #72 0x01143291 in funcall_lambda (fun=XIL(0xa000000001323038),
      nargs=nargs@entry=2, arg_vector=arg_vector@entry=0x88d9e8) at eval.c:3059
  #73 0x011437c9 in Ffuncall (nargs=3, args=args@entry=0x88d9e0) at eval.c:2790
  #74 0x01189680 in exec_byte_code (bytestr=<optimized out>,
      vector=<optimized out>, maxdepth=<optimized out>,
      args_template=<optimized out>, nargs=<optimized out>, nargs@entry=1,
      args=<optimized out>, args@entry=0x88de08) at bytecode.c:630
  #75 0x011433ec in funcall_lambda (fun=<optimized out>, nargs=nargs@entry=1,
      arg_vector=arg_vector@entry=0x88de08) at eval.c:2977
  #76 0x011437c9 in Ffuncall (nargs=nargs@entry=2, args=args@entry=0x88de00)
      at eval.c:2790
  #77 0x0114393e in run_hook_wrapped_funcall (nargs=nargs@entry=2,
      args=args@entry=0x88de00) at eval.c:2503
  #78 0x01141a36 in run_hook_with_args (nargs=2, args=0x88de00,
      funcall=0x114390e <run_hook_wrapped_funcall>) at eval.c:2584
  #79 0x0114389b in Ffuncall (nargs=3, args=args@entry=0x88ddf8) at eval.c:2776
  #80 0x01189680 in exec_byte_code (bytestr=<optimized out>,
      vector=<optimized out>, maxdepth=<optimized out>,
      args_template=<optimized out>, nargs=<optimized out>, nargs@entry=2,
      args=<optimized out>, args@entry=0x88e150) at bytecode.c:630
  #81 0x011433ec in funcall_lambda (fun=<optimized out>, nargs=nargs@entry=2,
      arg_vector=arg_vector@entry=0x88e150) at eval.c:2977
  #82 0x011437c9 in Ffuncall (nargs=3, args=args@entry=0x88e148) at eval.c:2790
  #83 0x01189680 in exec_byte_code (bytestr=<optimized out>,
      vector=<optimized out>, maxdepth=<optimized out>,
      args_template=<optimized out>, nargs=<optimized out>, nargs@entry=2,
      args=<optimized out>, args@entry=0x88e5b0) at bytecode.c:630
  #84 0x011433ec in funcall_lambda (fun=<optimized out>, nargs=nargs@entry=2,
      arg_vector=arg_vector@entry=0x88e5b0) at eval.c:2977
  #85 0x011437c9 in Ffuncall (nargs=3, args=args@entry=0x88e5a8) at eval.c:2790
  #86 0x01189680 in exec_byte_code (bytestr=<optimized out>,
      vector=<optimized out>, maxdepth=<optimized out>,
      args_template=<optimized out>, nargs=<optimized out>, nargs@entry=1,
      args=<optimized out>, args@entry=0x88ea60) at bytecode.c:630
  #87 0x011433ec in funcall_lambda (fun=<optimized out>, nargs=nargs@entry=1,
      arg_vector=arg_vector@entry=0x88ea60) at eval.c:2977
  #88 0x011437c9 in Ffuncall (nargs=nargs@entry=2, args=args@entry=0x88ea58)
      at eval.c:2790
  #89 0x01145d67 in Fapply (nargs=2, args=0x88ea58) at eval.c:2353
  #90 0x0114389b in Ffuncall (nargs=3, args=args@entry=0x88ea50) at eval.c:2776
  #91 0x01189680 in exec_byte_code (bytestr=<optimized out>,
      vector=<optimized out>, maxdepth=<optimized out>,
      args_template=<optimized out>, nargs=<optimized out>, nargs@entry=1,
      args=<optimized out>, args@entry=0x88eda8) at bytecode.c:630
  #92 0x011433ec in funcall_lambda (fun=<optimized out>, nargs=nargs@entry=1,
      arg_vector=arg_vector@entry=0x88eda8) at eval.c:2977
  #93 0x011437c9 in Ffuncall (nargs=nargs@entry=2, args=args@entry=0x88eda0)
      at eval.c:2790
  #94 0x011439c7 in call1 (fn=XIL(0xcfc0), arg1=XIL(0xa00000000ae3af70))
      at eval.c:2627
  #95 0x010bfe7c in timer_check_2 (idle_timers=<optimized out>,
      timers=<optimized out>) at keyboard.c:4472
  #96 timer_check () at keyboard.c:4534
  #97 0x010c04a1 in readable_events (flags=flags@entry=1) at keyboard.c:3349
  #98 0x010c0e98 in get_input_pending (flags=flags@entry=1) at keyboard.c:6841
  #99 0x010c3a53 in detect_input_pending_run_timers (
      do_display=do_display@entry=true) at keyboard.c:9961
  #100 0x01195175 in wait_reading_process_output (time_limit=<optimized out>,
      nsecs=nsecs@entry=0, read_kbd=-1, do_display=do_display@entry=true,
      wait_for_cell=XIL(0), wait_proc=wait_proc@entry=0x0,
      just_wait_proc=just_wait_proc@entry=0) at process.c:5531
  #101 0x0100a69e in sit_for (timeout=make_number(22),
      reading=reading@entry=true, display_option=display_option@entry=1)
      at dispnew.c:5805
  #102 0x010c6c8c in read_char (commandflag=<optimized out>,
      commandflag@entry=1, map=<optimized out>, prev_event=<optimized out>,
      used_mouse_menu=<optimized out>, used_mouse_menu@entry=0x88f743,
      end_time=<optimized out>, end_time@entry=0x0) at keyboard.c:2723
  #103 0x010c8303 in read_key_sequence (keybuf=keybuf@entry=0x88f850,
      prompt=<optimized out>,
      dont_downcase_last=dont_downcase_last@entry=false,
      can_return_switch_frame=can_return_switch_frame@entry=true,
      fix_current_buffer=fix_current_buffer@entry=true,
      prevent_redisplay=prevent_redisplay@entry=false, bufsize=30)
      at keyboard.c:9157
  #104 0x010ca3e4 in command_loop_1 () at keyboard.c:1368
  #105 0x011415d2 in internal_condition_case (
      bfun=bfun@entry=0x10ca155 <command_loop_1>, handlers=XIL(0x4e90),
      hfun=hfun@entry=0x10bf040 <cmd_error>) at eval.c:1336
  #106 0x010b82a3 in command_loop_2 (ignore=XIL(0)) at keyboard.c:1110
  #107 0x01141574 in internal_catch (tag=XIL(0xd290),
      func=func@entry=0x10b827c <command_loop_2>, arg=XIL(0)) at eval.c:1101
  #108 0x010b824b in command_loop () at keyboard.c:1089
  #109 0x010bec14 in recursive_edit_1 () at keyboard.c:695
  #110 0x010bef02 in Frecursive_edit () at keyboard.c:766
  #111 0x01233073 in main (argc=<optimized out>, argv=<optimized out>)
      at emacs.c:1722

  Lisp Backtrace:
  "1+" (0x88acd0)
  "goto-char" (0x88adb8)
  "save-excursion" (0x88aee8)
  "cond" (0x88aff8)
  "setq" (0x88b138)
  "let" (0x88b2b8)
  "if" (0x88b3d8)
  "catch" (0x88b548)
  "while" (0x88b668)
  "if" (0x88b788)
  "save-restriction" (0x88b8b8)
  "let" (0x88baf8)
  "c-beginning-of-statement-1" (0x88bbf0)
  "setq" (0x88be48)
  "eq" (0x88bf38)
  "not" (0x88c028)
  "and" (0x88c138)
  "while" (0x88c258)
  "let" (0x88c3d8)
  "let*" (0x88c528)
  "catch" (0x88c698)
  "c-beginning-of-decl-1" (0x88c810)
  0xadfab48 PVEC_COMPILED
  "font-lock-fontify-keywords-region" (0x88d0c0)
  "font-lock-default-fontify-region" (0x88d410)
  "c-font-lock-fontify-region" (0x88d740)
  "font-lock-fontify-region" (0x88d9e8)
  0x18b47060 PVEC_COMPILED
  "run-hook-wrapped" (0x88de00)
  "jit-lock--run-functions" (0x88e150)
  "jit-lock-fontify-now" (0x88e5b0)
  "jit-lock-stealth-fontify" (0x88ea60)
  "apply" (0x88ea58)
  "timer-event-handler" (0x88eda8)

This comes from the following code fragment:

  (defun c-beginning-of-statement-1 (&optional lim ignore-labels
					       noerror comma-delim)
  [...]
			   ;; Just gone back over some paren block?
			   ((looking-at "\\s(")
			    (save-excursion
			      (goto-char (1+ (c-down-list-backward
					      before-sws-pos)))
			      (c-crosses-statement-barrier-p
			       (point) maybe-after-boundary-pos)))

c-down-list-backward is documented to be able to return nil, so
passing the result to 1+ is unsafe.

I cannot claim a good understanding of the code, but the following
ad-hoc patch fixes the problem for me:

--- lisp/progmodes/cc-engine.el~0	2019-01-07 16:26:06.000000000 +0200
+++ lisp/progmodes/cc-engine.el	2019-05-01 14:43:35.823456200 +0300
@@ -1130,10 +1130,12 @@
 			 ;; Just gone back over some paren block?
 			 ((looking-at "\\s(")
 			  (save-excursion
-			    (goto-char (1+ (c-down-list-backward
-					    before-sws-pos)))
-			    (c-crosses-statement-barrier-p
-			     (point) maybe-after-boundary-pos)))
+			    (let ((pos1 (c-down-list-backward
+					 before-sws-pos)))
+			      (when (number-or-marker-p pos1)
+				(goto-char (1+ pos1))
+				(c-crosses-statement-barrier-p
+				 (point) maybe-after-boundary-pos)))))
 			 ;; Just gone back over an ordinary symbol of some sort?
 			 (t (c-crosses-statement-barrier-p
 			     (point) maybe-after-boundary-pos))))