This is an automated email from the git hooks/post-receive script.

git pushed a commit to branch tymux-10-quiet-head
in repository terminology.

View the commit online.

commit f0928ef96f4f3ad3d057fe552668f0ee2ef95732
Author: Boris Faure <[email protected]>
AuthorDate: Wed Nov 4 21:56:55 2026 +0000

    termptyesc: consume an unterminated APC as it arrives
    
    An APC has no length limit and is discarded whole, yet it was buffered
    until its ST and rescanned from its start on every read: quadratic in
    time and unbounded in memory. A 1 MiB APC took over two minutes read 3
    bytes at a time. Found by AFL.
    
    Skip it across reads as overlong OSCs are, keeping only a final ESC
    that may start the ST. Nothing of the APC shows, as before.
---
 src/bin/termpty.h    |  1 +
 src/bin/termptyesc.c | 26 +++++++++++++++++++++-----
 tests/apc-long.sh    | 40 ++++++++++++++++++++++++++++++++++++++++
 tests/tests.results  |  1 +
 4 files changed, 63 insertions(+), 5 deletions(-)

diff --git a/src/bin/termpty.h b/src/bin/termpty.h
index b2285c67..5da25342 100644
--- a/src/bin/termpty.h
+++ b/src/bin/termpty.h
@@ -272,6 +272,7 @@ struct tag_Termpty
    unsigned int osc_skip : 1; /* discarding an OSC too long for the buffer */
    unsigned int osc_skip_esc : 1; /* and the last codepoint was ESC */
    unsigned int dcs_skip : 1; /* discarding a DCS too long for the buffer */
+   unsigned int apc_skip : 1; /* inside an APC split across reads */
    /* bumped on each palette rewrite (OSC 4); scopes the truecolour cache */
    unsigned int palette_generation;
    struct {
diff --git a/src/bin/termptyesc.c b/src/bin/termptyesc.c
index e7e6a813..6123244d 100644
--- a/src/bin/termptyesc.c
+++ b/src/bin/termptyesc.c
@@ -5578,6 +5578,9 @@ err:
     return cc - c;
 }
 
+/* An unterminated APC is consumed as it arrives, with ty->apc_skip set for
+ * the next read to resume it: buffered until its ST, it would be rescanned
+ * whole on every read. */
 static int
 _handle_esc_apc(Termpty *ty, const Eina_Unicode *c, const Eina_Unicode *ce)
 {
@@ -5591,18 +5594,26 @@ _handle_esc_apc(Termpty *ty, const Eina_Unicode *c, const Eina_Unicode *ce)
              cc++;
              goto found;
           }
-        /* An ESC at the very end may yet become a ST: wait for more data. */
-        if ((*cc == ESC) && (cc + 1 < ce) && (*(cc + 1) == '\\'))
+        if (*cc == ESC)
           {
-             cc += 2;
-             goto found;
+             /* An ESC at the very end may yet become a ST: keep it. */
+             if (cc + 1 == ce)
+               break;
+             if (*(cc + 1) == '\\')
+               {
+                  cc += 2;
+                  goto found;
+               }
           }
         cc++;
      }
 
-   return 0;
+   if (cc > c)
+     ty->apc_skip = 1;
+   return cc - c;
 
 found:
+   ty->apc_skip = 0;
    WRN("unhandled application program command");
    ty->decoding_error = EINA_TRUE;
    return cc - c;
@@ -6343,6 +6354,11 @@ termpty_handle_seq(Termpty *ty, const Eina_Unicode *c, const Eina_Unicode *ce)
         len = _handle_dcs_skip(ty, c, ce);
         goto end;
      }
+   if (ty->apc_skip)
+     {
+        len = _handle_esc_apc(ty, c, ce);
+        goto end;
+     }
    if (c[0] < 0x20)
      {
         switch (c[0])
diff --git a/tests/apc-long.sh b/tests/apc-long.sh
new file mode 100755
index 00000000..a98f1f7e
--- /dev/null
+++ b/tests/apc-long.sh
@@ -0,0 +1,40 @@
+#!/bin/sh
+# shellcheck source=tests/utils.sh
+. "$(dirname "$0")/utils.sh"
+
+# $1: the number of times to repeat $2
+repeat()
+{
+    head -c "$1" /dev/zero | tr '\0' "$2"
+}
+
+# clear screen
+printf '\033[2J'
+
+# move to 0; 0
+printf '\033[0;0H'
+
+# a 1 MiB APC: none of it shows
+printf '\033_Ga=T,f=100;'
+repeat 1048576 A
+printf '\033\\'
+printf 'A'
+
+# an ESC not followed by '\' does not end the APC
+printf '\033_G'
+repeat 65536 B
+printf '\033X'
+repeat 65536 B
+printf '\033\\'
+printf 'B'
+
+# a long APC introduced by the C1 APC, cancelled by CAN
+printf '\302\237G'
+repeat 65536 C
+printf '\030'
+printf 'C'
+
+# a long APC cancelled by SUB, the next sequence is parsed
+printf '\033_G'
+repeat 65536 D
+printf '\032\033[32mD'
diff --git a/tests/tests.results b/tests/tests.results
index 3d074fd3..c191b444 100644
--- a/tests/tests.results
+++ b/tests/tests.results
@@ -188,6 +188,7 @@ sgr-underline-subparam.sh 527b0d806a768efbae630bf276fa9f6c
 sgr-overline.sh f9b5f05cd6ef8a2f261f3cf14b8459d8
 apc.sh 2a091bf1bac87b941cc481d1c179768c
 apc-can.sh ff841245b69d38a77923109af148726b
+apc-long.sh 752bcd8d218ea96a107afe2826509ae2
 osc_full_buffer_split_st.sh 78fd4dd54f93d9aa4e2a5e021ae76201
 osc52_set_full_buffer.sh 09378a29a1f383a8f3d01e41dd906e4a
 osc52_query_selectors.sh 7f99629b4f7a64eb6c830170cf3fabcf

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.

Reply via email to