Subject: bug#36898: 27.0.50; c-clear-string-fences causes args-out-of-range error

From: Óscar Fuentes <[email-masked]>
Date: Fri, 02 Aug 2019 22:01:05 +0200

Since recently (a few months, max.) ws-butler-mode [1] no longer works
on C++ (probably C also) buffers. ws-butler tracks changes on a buffer
(adds a hook to after-change-functions) adding text properties on the
changed area and, upon saving (adds a hook to before-save-hook)
traverses those areas sanitizing whitespace (removes trailing
whitespace, converts spaces <-> tabs, etc).

For replicating the problem, start with emacs -Q, visit a C++ file, load
ws-butler, enable it with M-x ws-butler-mode, put the point after an
inner `}', press enter to create a new empty line which is indented,
then save the buffer. ws-butler shall remove the white space on the new
line, but it doesn't. If the operation is excutend with debug-on-error
enabled, this trace appears:

Debugger entered--Lisp error: (args-out-of-range 1 1)
  get-text-property(1 c-fl-syn-tab)
  c-clear-string-fences()
  #f(compiled-function () #<bytecode 0x15768654dea9>)()
  c-after-change(804 804 2)
  delete-horizontal-space()
  (while (not (eobp)) (if (and ws-butler-convert-leading-tabs-or-spaces (not (and (boundp 'smart-tabs-mode) smart-tabs-mode))) (progn (let ((eol (point-at-eol))) (if indent-tabs-mode (progn (skip-chars-forward "\11" eol) (if (eq ... 32) (progn ...))) (skip-chars-forward " " eol) (if (eq (char-after) 9) (progn (untabify ... ...))))))) (end-of-line) (delete-horizontal-space) (forward-line 1))
  (save-restriction (narrow-to-region beg end) (goto-char (point-min)) (while (not (eobp)) (if (and ws-butler-convert-leading-tabs-or-spaces (not (and (boundp 'smart-tabs-mode) smart-tabs-mode))) (progn (let ((eol (point-at-eol))) (if indent-tabs-mode (progn (skip-chars-forward "\11" eol) (if ... ...)) (skip-chars-forward " " eol) (if (eq ... 9) (progn ...)))))) (end-of-line) (delete-horizontal-space) (forward-line 1)))
  (save-excursion (save-restriction (narrow-to-region beg end) (goto-char (point-min)) (while (not (eobp)) (if (and ws-butler-convert-leading-tabs-or-spaces (not (and (boundp ...) smart-tabs-mode))) (progn (let ((eol ...)) (if indent-tabs-mode (progn ... ...) (skip-chars-forward " " eol) (if ... ...))))) (end-of-line) (delete-horizontal-space) (forward-line 1))))
  (let ((ws-butler-saved t)) (save-excursion (save-restriction (narrow-to-region beg end) (goto-char (point-min)) (while (not (eobp)) (if (and ws-butler-convert-leading-tabs-or-spaces (not (and ... smart-tabs-mode))) (progn (let (...) (if indent-tabs-mode ... ... ...)))) (end-of-line) (delete-horizontal-space) (forward-line 1)))))
  (if (and (boundp 'ws-butler-saved) ws-butler-saved) (progn (narrow-to-region beg end) (goto-char (point-min)) (while (not (eobp)) (if (and ws-butler-convert-leading-tabs-or-spaces (not (and (boundp ...) smart-tabs-mode))) (progn (let ((eol ...)) (if indent-tabs-mode (progn ... ...) (skip-chars-forward " " eol) (if ... ...))))) (end-of-line) (delete-horizontal-space) (forward-line 1))) (let ((ws-butler-saved t)) (save-excursion (save-restriction (narrow-to-region beg end) (goto-char (point-min)) (while (not (eobp)) (if (and ws-butler-convert-leading-tabs-or-spaces (not ...)) (progn (let ... ...))) (end-of-line) (delete-horizontal-space) (forward-line 1))))))
  ws-butler-clean-region(800 806)
  (progn (ws-butler-clean-region beg end))
  (if (funcall ws-butler-trim-predicate beg end) (progn (ws-butler-clean-region beg end)))
  (lambda (_prop beg end) (save-excursion (setq beg (progn (goto-char beg) (point-at-bol)) end (progn (goto-char (1- end)) (point-at-eol)))) (if (funcall ws-butler-trim-predicate beg end) (progn (ws-butler-clean-region beg end))) (setq last-end end))(chg 803 806)
  funcall((lambda (_prop beg end) (save-excursion (setq beg (progn (goto-char beg) (point-at-bol)) end (progn (goto-char (1- end)) (point-at-eol)))) (if (funcall ws-butler-trim-predicate beg end) (progn (ws-butler-clean-region beg end))) (setq last-end end)) chg 803 806)
  (if prop (funcall func prop start (or end limit)))
  (while (and start (< start limit)) (setq prop (get-text-property start 'ws-butler-chg)) (setq end (text-property-not-all start limit 'ws-butler-chg prop)) (if prop (funcall func prop start (or end limit))) (setq start end))
  (let ((start (or start-position (point-min))) (limit (copy-marker (or end-position (point-max)))) prop end) (while (and start (< start limit)) (setq prop (get-text-property start 'ws-butler-chg)) (setq end (text-property-not-all start limit 'ws-butler-chg prop)) (if prop (funcall func prop start (or end limit))) (setq start end)) (set-marker limit nil))
  ws-butler-map-changes((lambda (_prop beg end) (save-excursion (setq beg (progn (goto-char beg) (point-at-bol)) end (progn (goto-char (1- end)) (point-at-eol)))) (if (funcall ws-butler-trim-predicate beg end) (progn (ws-butler-clean-region beg end))) (setq last-end end)))
  (let (last-end) (ws-butler-map-changes #'(lambda (_prop beg end) (save-excursion (setq beg (progn (goto-char beg) (point-at-bol)) end (progn (goto-char ...) (point-at-eol)))) (if (funcall ws-butler-trim-predicate beg end) (progn (ws-butler-clean-region beg end))) (setq last-end end))) (ws-butler-maybe-trim-eob-lines last-end))
  ws-butler-before-save()
  run-hooks(before-save-hook)
  basic-save-buffer(t)
  save-buffer(1)
  funcall-interactively(save-buffer 1)
  call-interactively(save-buffer nil nil)
  command-execute(save-buffer)

I tried to replicate the error without ws-butler, just adding
delete-horizontal-space to before-save-hook, but that does not throw the
error. ws-butler does a narrow-to-region, though.

In the meantime, I'm working-around the problem on ws-butler.es with

-     (delete-horizontal-space)
+     (ignore-errors (delete-horizontal-space)
+		    (delete-horizontal-space))

1. https://raw.githubusercontent.com/lewang/ws-butler/master/ws-butler.el

In GNU Emacs 27.0.50 (build 2, x86_64-pc-linux-gnu, X toolkit)
 of 2019-07-28 built on sky
Repository revision: 77ee23d1ede9eece3eab4cc67d7f2e72d30a1117
Repository branch: master
Windowing system distributor 'The X.Org Foundation', version 11.0.12004000
System Description: Debian GNU/Linux bullseye/sid