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

From: Noam Postavsky <[email-masked]>
Date: Thu, 30 May 2019 19:03:54 -0400

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

So, this, I guess.

>From 6fd6605c63ecc031a3fd6ba8b8e2e754c183b3f2 Mon Sep 17 00:00:00 2001
From: Noam Postavsky <[email-masked]>
Date: Thu, 30 May 2019 19:00:31 -0400
Subject: [PATCH] Remove unreliable test for match data clobbering (Bug#35264)

* src/search.c (Freplace_match): Don't test for change in search_regs
start and end, this is unreliable if change hooks modify text earlier
in the buffer.
---
 src/search.c | 18 +++++-------------
 1 file changed, 5 insertions(+), 13 deletions(-)

diff --git a/src/search.c b/src/search.c
index db7fecd9ba..1d4550849e 100644
--- a/src/search.c
+++ b/src/search.c
@@ -2730,22 +2730,16 @@ 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];
+     error out since otherwise this will 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.  */
   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);
-  /* Update saved data to match adjustment made by replace_range.  */
-  {
-    ptrdiff_t change = newpoint - sub_end;
-    if (sub_start >= sub_end)
-      sub_start += change;
-    sub_end += change;
-  }
 
   if (case_action == all_caps)
     Fupcase_region (make_number (search_regs.start[sub]),
@@ -2755,9 +2749,7 @@ DEFUN ("replace-match", Freplace_match, Sreplace_match, 1, 5, 0,
     Fupcase_initials_region (make_number (search_regs.start[sub]),
 			     make_number (newpoint));
 
-  if (search_regs.start[sub] != sub_start
-      || search_regs.end[sub] != sub_end
-      || search_regs.num_regs != num_regs)
+  if (search_regs.num_regs != num_regs)
     error ("Match data clobbered by buffer modification hooks");
 
   /* Put point back where it was in the text.  */
-- 
2.11.0