Subject: Re: bug#41520: 28.0.50; Crash in character.h due to assertion error

From: Pip Cet <[email-masked]>
Date: Mon, 25 May 2020 07:41:54 +0000

On Mon, May 25, 2020 at 7:28 AM Pip Cet <[email-masked]> wrote:
> On Mon, May 25, 2020 at 7:06 AM Stefan Kangas <[email-masked]> wrote:
> There's at least one other place that has the same error, so I'll grep
> some more before sending a patch.

Patch attached (it compiles, but I haven't tested it thoroughly). This
should fix two bugs in xdisp.c and a bug in Fend_of_line.

All of these bugs were silently fixed by my pos_t conversion.

At the very least, FETCH_CHAR should be renamed so it's clear that the
right way to use it is FETCH_CHAR (bytepos), not FETCH_CHAR (charpos).
I suggest FETCH_CHAR_AT_BYTEPOS.
From 460ddd6c40940610548c74efbd3da9fa8b170f71 Mon Sep 17 00:00:00 2001
From: Pip Cet <[email-masked]>
Date: Mon, 25 May 2020 07:35:28 +0000
Subject: [PATCH] Fix assertion errors caused by bytepos/charpos confusion

* src/xdisp.c (Fbidi_find_overridden_directionality): Use
FETCH_BYTE, not FETCH_CHAR, for a decremented byte position.
(get_visually_first_element): Ditto.

* src/cmds.c (Fend_of_line): Use byte positions for FETCH_CHAR,
not char positions.
---
 src/cmds.c  | 4 ++--
 src/xdisp.c | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/src/cmds.c b/src/cmds.c
index 9f96f210b9..d198bafc17 100644
--- a/src/cmds.c
+++ b/src/cmds.c
@@ -194,7 +194,7 @@ DEFUN ("end-of-line", Fend_of_line, Send_of_line, 0, 1, "^p",
       SET_PT (newpos);
 
       if (PT > newpos
-	  && FETCH_CHAR (PT - 1) == '\n')
+	  && FETCH_CHAR (PT_BYTE - 1) == '\n')
 	{
 	  /* If we skipped over a newline that follows
 	     an invisible intangible run,
@@ -205,7 +205,7 @@ DEFUN ("end-of-line", Fend_of_line, Send_of_line, 0, 1, "^p",
 	  break;
 	}
       else if (PT > newpos && PT < ZV
-	       && FETCH_CHAR (PT) != '\n')
+	       && FETCH_CHAR (PT_BYTE) != '\n')
 	/* If we skipped something intangible
 	   and now we're not really at eol,
 	   keep going.  */
diff --git a/src/xdisp.c b/src/xdisp.c
index cf15f579b5..00c526874c 100644
--- a/src/xdisp.c
+++ b/src/xdisp.c
@@ -8290,7 +8290,7 @@ get_visually_first_element (struct it *it)
     }
   else if (it->bidi_it.charpos == bob
 	   || (!string_p
-	       && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
+	       && (FETCH_BYTE (it->bidi_it.bytepos - 1) == '\n'
 		   || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
     {
       /* If we are at the beginning of a line/string, we can produce
@@ -24182,7 +24182,7 @@ DEFUN ("bidi-find-overridden-directionality",
 	  itb.charpos = BEGV;
 	  itb.bytepos = BEGV_BYTE;
 	}
-      else if (FETCH_CHAR (from_bpos - 1) == '\n')
+      else if (FETCH_BYTE (from_bpos - 1) == '\n')
 	{
 	  itb.charpos = from_pos;
 	  itb.bytepos = from_bpos;
-- 
2.27.0.rc0