This patch fixes the bug in ESC sequence parser used when pseudo
console is enabled in pty_master_fwd_thread(). Previously, if
multiple ESC sequences exist in a fowarding chunk, the later one
might not be processed appropriately. In addition, the termination
ST (ESC \) was not supported, that is, only BEL was supported.

Fixes: 10d083c745dd ("Cygwin: pty: Inherit typeahead data between two input 
pipes.")
Reviewed-by: Corinna Vinschen <[email protected]>
Signed-off-by: Takashi Yano <[email protected]>
---
 winsup/cygwin/fhandler/pty.cc | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/winsup/cygwin/fhandler/pty.cc b/winsup/cygwin/fhandler/pty.cc
index 679068ea2..3b0b4f073 100644
--- a/winsup/cygwin/fhandler/pty.cc
+++ b/winsup/cygwin/fhandler/pty.cc
@@ -2680,7 +2680,7 @@ fhandler_pty_master::pty_master_fwd_thread (const 
master_fwd_thread_param_t *p)
          int state = 0;
          int start_at = 0;
          for (DWORD i=0; i<rlen; i++)
-           if (outbuf[i] == '\033')
+           if (state == 0 && outbuf[i] == '\033')
              {
                start_at = i;
                state = 1;
@@ -2688,12 +2688,14 @@ fhandler_pty_master::pty_master_fwd_thread (const 
master_fwd_thread_param_t *p)
              }
            else if ((state == 1 && outbuf[i] == ']') ||
                     (state == 2 && outbuf[i] == '0') ||
-                    (state == 3 && outbuf[i] == ';'))
+                    (state == 3 && outbuf[i] == ';') ||
+                    (state == 4 && outbuf[i] == '\033'))
              {
                state ++;
                continue;
              }
-           else if (state == 4 && outbuf[i] == '\a')
+           else if ((state == 4 && outbuf[i] == '\a')
+                    || (state == 5 && outbuf[i] == '\\'))
              {
                const char *helper_str = "\\bin\\cygwin-console-helper.exe";
                if (memmem (&outbuf[start_at], i + 1 - start_at,
@@ -2701,11 +2703,14 @@ fhandler_pty_master::pty_master_fwd_thread (const 
master_fwd_thread_param_t *p)
                  {
                    memmove (&outbuf[start_at], &outbuf[i+1], rlen-i-1);
                    rlen = wlen = start_at + rlen - i - 1;
+                   i = start_at - 1;
                  }
                state = 0;
                continue;
              }
-           else if (outbuf[i] == '\a')
+           else if (state == 4)
+             continue;
+           else
              {
                state = 0;
                continue;
-- 
2.51.0

Reply via email to