public inbox for cygwin-cvs@sourceware.org
help / color / mirror / Atom feed
From: Takashi Yano <tyan0@sourceware.org>
To: cygwin-cvs@sourceware.org
Subject: [newlib-cygwin/cygwin-3_3-branch] Cygwin: console: Prevent special keys processing from drop.
Date: Wed,  2 Mar 2022 01:16:51 +0000 (GMT)	[thread overview]
Message-ID: <20220302011651.7B5423858D39@sourceware.org> (raw)

https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=13d2572373f01384eb5c6a841a4a31b5d5463834

commit 13d2572373f01384eb5c6a841a4a31b5d5463834
Author: Takashi Yano <takashi.yano@nifty.ne.jp>
Date:   Wed Mar 2 09:55:52 2022 +0900

    Cygwin: console: Prevent special keys processing from drop.
    
    - There was a potential risk to drop special key processing when
      process_input_messsage() is called intermittently. This patch
      fixes the issue.

Diff:
---
 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 370d9f40b..1f3ddfcab 100644
--- a/winsup/cygwin/fhandler.h
+++ b/winsup/cygwin/fhandler.h
@@ -2066,6 +2066,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 656a46d89..472485106 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)
       }
   } m;
   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;
@@ -311,7 +310,7 @@ remove_record:
 	      i--;
 	    }
 	}
-      processed_up_to = total_read - 1;
+      con.num_processed = total_read;
       if (total_read)
 	{
 	  do
@@ -448,6 +447,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;
     }
 }
 
@@ -1263,14 +1263,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;
 }
 
@@ -1669,6 +1672,7 @@ fhandler_console::tcflush (int queue)
 	  __seterrno ();
 	  res = -1;
 	}
+      con.num_processed = 0;
       release_attach_mutex ();
     }
   return res;


                 reply	other threads:[~2022-03-02  1:16 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220302011651.7B5423858D39@sourceware.org \
    --to=tyan0@sourceware.org \
    --cc=cygwin-cvs@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).