GNU bug report logs - #35264
"Match data clobbered by buffer modification hooks" when hooks only shifted match-data's markers

Previous Next

Package: emacs;

Reported by: Noam Postavsky <npostavs <at> gmail.com>

Date: Sun, 14 Apr 2019 04:41:01 UTC

Severity: normal

Tags: fixed, patch

Fixed in version 28.1

Done: Lars Ingebrigtsen <larsi <at> gnus.org>

Bug is archived. No further changes may be made.

Full log


View this message in rfc822 format

From: Noam Postavsky <npostavs <at> gmail.com>
To: 35264 <at> debbugs.gnu.org
Cc: stefan monnier <monnier <at> iro.umontreal.ca>
Subject: bug#35264: "Match data clobbered by buffer modification hooks" when hooks only shifted match-data's markers
Date: Sun, 14 Apr 2019 00:40:13 -0400
[Message part 1 (text/plain, inline)]
Version: 27.0.50 26.2 25.3
X-Debbugs-CC: Stefan Monnier <monnier <at> iro.umontreal.ca>

emacs -Q -l bug-xxxx-match-data-marker-clobber.el

Hit <f12>, gives

Debugger entered--Lisp error: (error "Match data clobbered by buffer modification hooks")
  replace-match("ABCDEF" t t)
  (let* ((after-change-functions (list (function (lambda (&rest _) (let (... ...) (save-excursion ...))))))) (search-backward "abcdef") (replace-match "ABCDEF" t t))
  (save-current-buffer (set-buffer (get-buffer-create "*test*")) (display-buffer (current-buffer)) (erase-buffer) (insert "1234567890\n") (insert "abcdefghilk\n") (make-local-variable (quote after-change-functions)) (let* ((after-change-functions (list (function (lambda (&rest _) (let ... ...)))))) (search-backward "abcdef") (replace-match "ABCDEF" t t)))
  bug-match-data-marker-clobber()
  funcall-interactively(bug-match-data-marker-clobber)
  call-interactively(bug-match-data-marker-clobber nil nil)
  command-execute(bug-match-data-marker-clobber)

But the modification hook in question did call save-match-data.  As far
as I can tell, the problem is that the match-data consists of markers,
whose position gets shifted by deletion of characters.  The check for
this error uses simple integers, so there's no way it can account for
this.

I think this is a variant of Bug#23917, there was some talk there about
removing the check, perhaps that is the right solution.

DEFUN ("replace-match", Freplace_match, Sreplace_match, 1, 5, 0,
  [...]
  /* The functions below modify the buffer, so they could trigger
     various modification hooks (see signal_before_change and
     signal_after_change).  If these hooks clobber the match data we
     error out since otherwise this will result in confusing bugs.  */
  ptrdiff_t sub_start = search_regs.start[sub];
  ptrdiff_t sub_end = search_regs.end[sub];
  unsigned  num_regs = search_regs.num_regs;
  newpoint = search_regs.start[sub] + SCHARS (newtext);

  /* Replace the old text with the new in the cleanest possible way.  */
  replace_range (search_regs.start[sub], search_regs.end[sub],
                 newtext, 1, 0, 1, 1);
  [...]
  if (search_regs.start[sub] != sub_start
      || search_regs.end[sub] != sub_end
      || search_regs.num_regs != num_regs)
    error ("Match data clobbered by buffer modification hooks");


bug-xxxx-match-data-marker-clobber.el:

[bug-xxxx-match-data-marker-clobber.el (text/plain, inline)]
(defun bug-match-data-marker-clobber ()
  (interactive)
  (with-current-buffer (get-buffer-create "*test*")
    (display-buffer (current-buffer))
    (erase-buffer)
    (insert "1234567890\n")
    (insert "abcdefghilk\n")
    (make-local-variable 'after-change-functions)
    (let* ((after-change-functions `
            (,(lambda (&rest _)
                (let ((inhibit-modification-hooks nil)
                      (after-change-functions nil))
                  (save-excursion
                    (save-match-data
                      (goto-char (point-min))
                      (looking-at "[0-9]")
                      (delete-char 1))
                    ;; match-data is restored, but markers have a
                    ;; different position now, because of the
                    ;; deletion.
                    ;;
                    ;; (match-data) ;=> (#<marker <at> 11> #<marker <at> 17>)
                    ))))))
      (search-backward "abcdef")
      ;; (match-data) ;=> (#<marker <at> 12> #<marker <at> 18>)
      (replace-match "ABCDEF" t t) ;; Triggers `after-change-functions'.
      )))

(setq debug-on-error t)
(define-key global-map [f12] 'bug-match-data-marker-clobber)


This bug report was last modified 4 years and 2 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.