Subject: bug#79200: 31.0.50; Duplicated elements for '#<marker at' in buffer-undo-list

From: Gerd Möllmann <[email-masked]>
Date: Tue, 12 Aug 2025 17:47:31 +0200

Gerd Möllmann <[email-masked]> writes:

> Helmut Eller <[email-masked]> writes:
>
>> On Tue, Aug 12 2025, Gerd Möllmann wrote:
>>
>>> +static ptrdiff_t
>>> +next_marker_id (void)
>>> +{
>>> +  static ptrdiff_t next = 0;
>>> +  return next++;
>>> +}
>> [...]
>>>    DUMP_FIELD_COPY (out, marker, insertion_type);
>>> +  DUMP_FIELD_COPY (out, marker, id);
>>> +  out->id *= -1;
>>
>> I wonder if we shouldn't simply dump and restore the next id counter
>> (with pdumper_remember_scalar).
>
> Yeah, I think that's better.
>
> The above isn't correct anyway if we dump an Emacs that has a dump
> loaded. In that case the marker to be dumped could have a negative id
> already, and so on.

Like so, I think:

>From c2032d15361944cc172b33eec34939e650ad680e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Gerd=20M=C3=B6llmann?= <[email-masked]>
Date: Tue, 12 Aug 2025 17:45:11 +0200
Subject: [PATCH] Don't use negative marker ids

* src/alloc.c (syms_of_alloc): New DEFVAR_INT for next marker id.
(next_marker_id): Use it.
* src/pdumper.c (dump_marker): Store id as-is.
---
 src/alloc.c   | 9 +++++++--
 src/pdumper.c | 2 +-
 2 files changed, 8 insertions(+), 3 deletions(-)

diff --git a/src/alloc.c b/src/alloc.c
index 3ab0b5357c1..63af4443b2e 100644
--- a/src/alloc.c
+++ b/src/alloc.c
@@ -3947,8 +3947,9 @@ build_overlay (bool front_advance, bool rear_advance,
 static ptrdiff_t
 next_marker_id (void)
 {
-  static ptrdiff_t next = 0;
-  return next++;
+  if (next__marker_id == PTRDIFF_MAX)
+    next__marker_id = 0;
+  return next__marker_id++;
 }
 
 DEFUN ("make-marker", Fmake_marker, Smake_marker, 0, 0, 0,
@@ -7865,6 +7866,10 @@ syms_of_alloc (void)
 #endif
   DEFSYM (Qalloc, "alloc");
   DEFSYM (QCemergency, ":emergency");
+
+  DEFVAR_INT ("next--marker-id", next__marker_id,
+	      doc: /* Next marker id (internal use only..  */);
+  next__marker_id = 0;
 }
 
 #ifdef HAVE_MPS
diff --git a/src/pdumper.c b/src/pdumper.c
index b9583a69b3d..a832ecfba4b 100644
--- a/src/pdumper.c
+++ b/src/pdumper.c
@@ -2229,7 +2229,7 @@ dump_marker (struct dump_context *ctx, const struct Lisp_Marker *marker)
   DUMP_FIELD_COPY (out, marker, need_adjustment);
   DUMP_FIELD_COPY (out, marker, insertion_type);
   DUMP_FIELD_COPY (out, marker, id);
-  out->id *= -1;
+
   if (marker->buffer)
     {
       dump_field_lv_rawptr (ctx, out, marker, &marker->buffer,
-- 
2.50.1