From 8b458aea86ccbba255cbc5e97d6426df1e8bc6d9 Mon Sep 17 00:00:00 2001 From: Eli Zaretskii Date: Wed, 15 Nov 2023 15:15:35 +0200 Subject: [PATCH] Fix query-replace at EOB * lisp/replace.el (replace-match-maybe-edit): Avoid clobbering match-data with outdated buffer position. (Bug#67124) --- lisp/replace.el | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lisp/replace.el b/lisp/replace.el index 6b06e48c384..7fec54ecb27 100644 --- a/lisp/replace.el +++ b/lisp/replace.el @@ -2644,8 +2644,11 @@ passed in. If LITERAL is set, no checking is done, anyway." (replace-match newtext fixedcase literal) ;; `query-replace' undo feature needs the beginning of the match position, ;; but `replace-match' may change it, for instance, with a regexp like "^". - ;; Ensure that this function preserves the match data (Bug#31492). - (set-match-data match-data) + ;; Ensure that this function preserves the beginning of the match position + ;; (bug#31492). But we need to avoid clobbering the end of the match with + ;; the original match-end position, since `replace-match' could have made + ;; that incorrect or even invalid (bug#67124). + (set-match-data (list (car match-data) (nth 1 (match-data)))) ;; `replace-match' leaves point at the end of the replacement text, ;; so move point to the beginning when replacing backward. (when backward (goto-char (nth 0 match-data))) -- 2.39.2