- There was a potential risk to drop special key processing when
  process_input_messsage() is called intermittently. This patch
  fixes the issue.
---
 winsup/cygwin/fhandler.h          |  2 ++
 winsup/cygwin/fhandler_console.cc | 22 +++++++++++++---------
 2 files changed, 15 insertions(+), 9 deletions(-)

diff --git a/winsup/cygwin/fhandler.h b/winsup/cygwin/fhandler.h
index dd1ab0422..c5eb62136 100644
--- a/winsup/cygwin/fhandler.h
+++ b/winsup/cygwin/fhandler.h
@@ -2067,6 +2067,8 @@ class dev_console
   char *cons_rapoi;
   bool cursor_key_app_mode;
   bool disable_master_thread;
+  int num_processed; /* Number of input events in the current input buffer
+                       already processed by cons_master_thread(). */
 
   inline UINT get_console_cp ();
   DWORD con_to_str (char *d, int dlen, WCHAR w);
diff --git a/winsup/cygwin/fhandler_console.cc 
b/winsup/cygwin/fhandler_console.cc
index 2a4aa7a70..7e51ea19e 100644
--- a/winsup/cygwin/fhandler_console.cc
+++ b/winsup/cygwin/fhandler_console.cc
@@ -200,7 +200,6 @@ fhandler_console::cons_master_thread (handle_set_t *p, tty 
*ttyp)
       }
   };
   termios &ti = ttyp->ti;
-  int processed_up_to = -1;
   while (con.owner == myself->pid)
     {
       DWORD total_read, n, i;
@@ -223,17 +222,17 @@ fhandler_console::cons_master_thread (handle_set_t *p, 
tty *ttyp)
          if (total_read == INREC_SIZE /* Working space full */
              && cygwait (p->input_handle, (DWORD) 0) == WAIT_OBJECT_0)
            {
-             const int incr = min (processed_up_to + 1, additional_space);
+             const int incr = min (con.num_processed, additional_space);
              ReadConsoleInputW (p->input_handle,
                                 input_rec + total_read, incr, &n);
              /* Discard oldest n events. */
              memmove (input_rec, input_rec + n, m::bytes (total_read));
-             processed_up_to -= n;
+             con.num_processed -= n;
              nowait = true;
            }
          break;
        case WAIT_TIMEOUT:
-         processed_up_to = -1;
+         con.num_processed = 0;
        case WAIT_SIGNALED:
        case WAIT_CANCELED:
          break;
@@ -256,7 +255,7 @@ fhandler_console::cons_master_thread (handle_set_t *p, tty 
*ttyp)
              ttyp->kill_pgrp (SIGWINCH);
            }
        }
-      for (i = processed_up_to + 1; i < total_read; i++)
+      for (i = con.num_processed; i < total_read; i++)
        {
          wchar_t wc;
          char c;
@@ -279,7 +278,7 @@ fhandler_console::cons_master_thread (handle_set_t *p, tty 
*ttyp)
                  ttyp->output_stopped = false;
                  if (ti.c_lflag & NOFLSH)
                    goto remove_record;
-                 processed_up_to = -1;
+                 con.num_processed = 0;
                  goto skip_writeback;
                default: /* not signalled */
                  break;
@@ -312,7 +311,7 @@ remove_record:
              i--;
            }
        }
-      processed_up_to = total_read - 1;
+      con.num_processed = total_read;
       if (total_read)
        {
          do
@@ -449,6 +448,7 @@ fhandler_console::setup ()
       shared_console_info->tty_min_state.is_console = true;
       con.cursor_key_app_mode = false;
       con.disable_master_thread = true;
+      con.num_processed = 0;
     }
 }
 
@@ -1265,14 +1265,17 @@ fhandler_console::process_input_message (void)
     }
 out:
   /* Discard processed recored. */
-  DWORD dummy;
   DWORD discard_len = min (total_read, i + 1);
   /* If input is signalled, do not discard input here because
      tcflush() is already called from line_edit(). */
   if (stat == input_signalled && !(ti->c_lflag & NOFLSH))
     discard_len = 0;
   if (discard_len)
-    ReadConsoleInputW (get_handle (), input_rec, discard_len, &dummy);
+    {
+      DWORD discarded;
+      ReadConsoleInputW (get_handle (), input_rec, discard_len, &discarded);
+      con.num_processed = max (con.num_processed - discarded, 0);
+    }
   return stat;
 }
 
@@ -1671,6 +1674,7 @@ fhandler_console::tcflush (int queue)
          __seterrno ();
          res = -1;
        }
+      con.num_processed = 0;
       release_attach_mutex ();
     }
   return res;
-- 
2.35.1

Reply via email to