Subject: bug#60467: 30.0.50; primitive-undo: Changes to be undone by function different from announced

From: Stefan Monnier <[email-masked]>
Date: Tue, 03 Jan 2023 11:32:13 -0500

> With that patch, the "combine-change-calls: buffer-undo-list broken" message
> would be displayed with the recipe of this bug report.

IIUC this is a separate issue which is fairly easy to fix (and doesn't
need to be fixed on `emacs-29`) with something like the patch below.

> And timestamp entries would be added to what is "body-undo-list" in my
> patch. It's not clear to me that this could cause problems, but
> I guess it's safer to not include them, given that they were never
> included,

AFAICT when they were not included, they resulted in a broken undo
entry (as evidenced by the current bug report), so anything we do in
their presence is "safer" than what we did before :-)

Let's wait to see what Alan has to say,

        Stefan

diff --git a/lisp/subr.el b/lisp/subr.el
index 5fb150994ec..ff3a7c403d0 100644
--- a/lisp/subr.el
+++ b/lisp/subr.el
@@ -4970,15 +4970,16 @@ combine-change-calls-1
 		(progn
 		  (while (and (not (eq (cdr ptr) old-bul))
 			      ;; In case garbage collection has removed OLD-BUL.
-			      (cdr ptr)
+			      (or (cdr ptr)
+			          (progn
+			            (message "combine-change-calls: buffer-undo-list broken")
+			            nil))
 			      ;; Don't include a timestamp entry.
 			      (not (and (consp (cdr ptr))
 					(consp (cadr ptr))
 					(eq (caadr ptr) t)
 					(setq old-bul (cdr ptr)))))
 		    (setq ptr (cdr ptr)))
-		  (unless (cdr ptr)
-		    (message "combine-change-calls: buffer-undo-list broken"))
 		  (setcdr ptr nil)
 		  (push ap-elt buffer-undo-list)
 		  (setcdr buffer-undo-list old-bul)))))