Subject: Re: bug#29918: 26.0.90; serial-term error in process filter

From: Noam Postavsky <[email-masked]>
Date: Thu, 18 Jul 2019 07:22:10 -0400

Eli Zaretskii <[email-masked]> writes:

>> there is a possibility it prevents correct decoding in case the
>> terminal receives a utf-8 character in single byte chunks (though
>> I'm not sure if it's even valid for a program to emit single bytes
>> like that).
>
> I agree.

I thought of limiting partial to 4, to avoid this possibility, since
utf-8 characters can be 4 bytes at most (not sure about other encodings
though).  

>From bad839ca47ac58bd4967b95f447a596940e832f1 Mon Sep 17 00:00:00 2001
From: Noam Postavsky <[email-masked]>
Date: Wed, 17 Jul 2019 07:20:20 -0400
Subject: [PATCH] Handle completely undecoded input in term (Bug#29918)

* lisp/term.el (term-emulate-terminal): Avoid errors if the whole
decoded string is eight-bit characters.  Accumulate at most 4
undecoded bytes.
---
 lisp/term.el | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/lisp/term.el b/lisp/term.el
index cbef68dc0a..2fdd3253ec 100644
--- a/lisp/term.el
+++ b/lisp/term.el
@@ -2900,9 +2900,11 @@ term-emulate-terminal
                           ;; next time.
                           (when (= funny str-length)
                             (let ((partial 0))
-                              (while (eq (char-charset (aref decoded-substring
-                                                             (- count 1 partial)))
-                                         'eight-bit)
+                              (while (and (< partial count)
+                                          (< partial 4)
+                                          (eq (char-charset (aref decoded-substring
+                                                                  (- count 1 partial)))
+                                              'eight-bit))
                                 (cl-incf partial))
                               (when (> partial 0)
                                 (setq term-terminal-undecoded-bytes
-- 
2.11.0