Subject: bug#44349: 28.0.50; Assertion failure on macOS when resizing frame

From: Alan Third <[email-masked]>
Date: Fri, 27 Nov 2020 22:24:29 +0000

On Sat, Oct 31, 2020 at 06:52:08PM +0200, Eli Zaretskii wrote:
> > From: Philipp Stephani <[email-masked]>
> > Date: Sat, 31 Oct 2020 17:43:57 +0100
> > Cc: [email-masked]
> > 
> > Am Sa., 31. Okt. 2020 um 17:35 Uhr schrieb Eli Zaretskii <[email-masked]>:
> > >
> > > > From: Philipp Stephani <[email-masked]>
> > > > Date: Sat, 31 Oct 2020 16:57:54 +0100
> > > >
> > > > > doprnt.c:493: Emacs fatal error: assertion failed: ASCII_CHAR_P (fmtchar)
> > > > > Fatal error 6: Aborted
> > > > > Abort trap: 6
> > >
> > > I cannot say I understand why that assertion is there.
> > 
> > Line 81 of doprnt.c states that non-ASCII characters aren't supported.
> 
> I don't see why we would want to enforce that, it sounds like a grave
> limitation.  Maybe I'm missing some background here.  The version we
> have on emacs-27 does support non-ASCII characters in the format.

Patch attached.

I can't see any other special cases that need to be handled and all my
tests worked, so I think this is all that's needed.

-- 
Alan Third
>From 7bf4318f34c77e90c76944ee28693a895baf1807 Mon Sep 17 00:00:00 2001
From: Alan Third <[email-masked]>
Date: Fri, 27 Nov 2020 22:11:47 +0000
Subject: [PATCH] Allow doprint to handle multibyte chars in format (bug#44349)

* src/doprnt.c (doprnt): Handle the case where fmtchar is the start of
a multibyte character.
---
 src/doprnt.c | 23 +++++++++++++++++++----
 1 file changed, 19 insertions(+), 4 deletions(-)

diff --git a/src/doprnt.c b/src/doprnt.c
index 9316497720..841b8249c7 100644
--- a/src/doprnt.c
+++ b/src/doprnt.c
@@ -77,8 +77,7 @@
    where flags is [+ -0], width is [0-9]+, precision is .[0-9]+, and length
    is empty or l or the value of the pD or pI or PRIdMAX (sans "d") macros.
    A % that does not introduce a valid %-sequence causes undefined behavior.
-   ASCII bytes in FORMAT other than % are copied through as-is;
-   non-ASCII bytes should not appear in FORMAT.
+   Bytes in FORMAT other than % are copied through as-is.
 
    The + flag character inserts a + before any positive number, while a space
    inserts a space before any positive number; these flags only affect %d, %o,
@@ -486,14 +485,30 @@ doprnt (char *buffer, ptrdiff_t bufsize, const char *format,
 	src = uLSQM, srclen = sizeof uLSQM - 1;
       else if (EQ (quoting_style, Qcurve) && fmtchar == '\'')
 	src = uRSQM, srclen = sizeof uRSQM - 1;
-      else
+      else if (! LEADING_CODE_P (fmtchar))
 	{
 	  if (EQ (quoting_style, Qstraight) && fmtchar == '`')
 	    fmtchar = '\'';
-	  eassert (ASCII_CHAR_P (fmtchar));
+
 	  *bufptr++ = fmtchar;
 	  continue;
 	}
+      else
+        {
+          src = fmt0;
+          srclen = BYTES_BY_CHAR_HEAD (fmtchar);
+
+          /* doprnt_non_null_end doesn't know about multibyte
+             characters so can truncate format in the middle of one.
+             If that happens just ignore that character.  */
+          for ( ; *fmt != 0 && fmt < src + srclen ; fmt++);
+
+          if (fmt < src + srclen)
+            {
+              *bufptr++ = '\0';
+              break;
+            }
+        }
 
       if (bufsize < srclen)
 	{
-- 
2.26.1