Subject: bug#35264: "Match data clobbered by buffer modification hooks" when hooks only shifted match-data's markers

From: Lars Ingebrigtsen <[email-masked]>
Date: Wed, 12 May 2021 16:35:38 +0200

Lars Ingebrigtsen <[email-masked]> writes:

>> 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.
>
> That does make sense, but removing this (somewhat buggy) sanity check is
> perhaps a bit scary.  Any comments on this?

There were no comments, and the patch no longer applies.  I've respun it
now for Emacs 28, and this fixes the case reported in this bug report
(which still reproduces in Emacs 28).

Any comments?

diff --git a/src/search.c b/src/search.c
index c757bf3d1f..df384e1dcf 100644
--- a/src/search.c
+++ b/src/search.c
@@ -2723,7 +2723,6 @@ DEFUN ("replace-match", Freplace_match, Sreplace_match, 1, 5, 0,
     }
 
   newpoint = sub_start + SCHARS (newtext);
-  ptrdiff_t newstart = sub_start == sub_end ? newpoint : sub_start;
 
   /* Replace the old text with the new in the cleanest possible way.  */
   replace_range (sub_start, sub_end, newtext, 1, 0, 1, true);
@@ -2739,11 +2738,11 @@ DEFUN ("replace-match", Freplace_match, Sreplace_match, 1, 5, 0,
   /* The replace_range etc. functions can trigger modification hooks
      (see signal_before_change and signal_after_change).  Try to error
      out if these hooks clobber the match data since clobbering can
-     result in confusing bugs.  Although this sanity check does not
-     catch all possible clobberings, it should catch many of them.  */
-  if (! (search_regs.num_regs == num_regs
-	 && search_regs.start[sub] == newstart
-	 && search_regs.end[sub] == newpoint))
+     result in confusing bugs.  We used to check for changes in
+     search_regs start and end, but that fails if modification hooks
+     remove or add text earlier in the buffer, so just check num_regs
+     now. */
+  if (search_regs.num_regs != num_regs)
     error ("Match data clobbered by buffer modification hooks");
 
   /* Put point back where it was in the text, if possible.  */

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no