bug-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug#35264: "Match data clobbered by buffer modification hooks" when hook


From: Noam Postavsky
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

Version: 27.0.50 26.2 25.3
X-Debbugs-CC: Stefan Monnier <monnier@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:

(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@11> #<marker@17>)
                    ))))))
      (search-backward "abcdef")
      ;; (match-data) ;=> (#<marker@12> #<marker@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)


reply via email to

[Prev in Thread] Current Thread [Next in Thread]