unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#35264: "Match data clobbered by buffer modification hooks" when hooks only shifted match-data's markers
@ 2019-04-14  4:40 Noam Postavsky
  2019-05-30 23:03 ` Noam Postavsky
  0 siblings, 1 reply; 14+ messages in thread
From: Noam Postavsky @ 2019-04-14  4:40 UTC (permalink / raw)
  To: 35264; +Cc: stefan monnier

[-- Attachment #1: Type: text/plain, Size: 2366 bytes --]

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:


[-- Attachment #2: bug reproducer --]
[-- Type: text/plain, Size: 1183 bytes --]

(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)


^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2021-05-16 13:20 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-04-14  4:40 bug#35264: "Match data clobbered by buffer modification hooks" when hooks only shifted match-data's markers Noam Postavsky
2019-05-30 23:03 ` Noam Postavsky
2020-10-02  4:47   ` Lars Ingebrigtsen
2021-05-12 14:35     ` Lars Ingebrigtsen
2021-05-12 14:55       ` Eli Zaretskii
2021-05-12 15:43         ` Lars Ingebrigtsen
2021-05-12 15:48           ` Eli Zaretskii
2021-05-12 15:57             ` Lars Ingebrigtsen
2021-05-12 16:03               ` Eli Zaretskii
2021-05-13  9:11                 ` Lars Ingebrigtsen
2021-05-13 10:14                   ` Eli Zaretskii
2021-05-13 16:03                     ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2021-05-13 17:09                       ` Eli Zaretskii
2021-05-16 13:20                 ` Lars Ingebrigtsen

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).