[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug#20783: 25.0.50; [PATCH] byte-to-position has internal off-by-one bug

From: Wolfgang Jenkner
Subject: bug#20783: 25.0.50; [PATCH] byte-to-position has internal off-by-one bug
Date: Wed, 10 Jun 2015 17:13:30 +0200
User-agent: Gnus/5.130014 (Ma Gnus v0.14) Emacs/25.0.50 (berkeley-unix)

Here's a test case for the bug:

  (insert "éé")
  (let ((i 1) pos res)
    (while (setq pos (byte-to-position i))
      (push pos res)
      (setq i (1+ i)))
    (nreverse res)))

=> (1 2 2 2 3)

while the correct result is

=> (1 1 2 2 3)

I found that this bug had been noticed before in


Here's a patch.  The fix may look a bit clumsy but it's actually meant
to avoid pessimizing the presumably common case where the initial
bytepos is at a character boundary.

-- >8 --
Subject: [PATCH] * src/marker.c (buf_bytepos_to_charpos): Fix best_below_byte

If bytepos is not after a character boundary the preceding loop
overshoots by one character position.
 src/marker.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/src/marker.c b/src/marker.c
index 73928ba..94d676b 100644
--- a/src/marker.c
+++ b/src/marker.c
@@ -341,6 +341,12 @@ buf_bytepos_to_charpos (struct buffer *b, ptrdiff_t 
          BUF_INC_POS (b, best_below_byte);
+      if (best_below_byte != bytepos)
+       {
+         best_below--;
+         BUF_DEC_POS (b, best_below_byte);
+       }
       /* If this position is quite far from the nearest known position,
         cache the correspondence by creating a marker here.
         It will last until the next GC.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]