public inbox for cygwin-patches@cygwin.com
 help / color / mirror / Atom feed
From: Mitchell Hentges <mhentges@mozilla.com>
To: cygwin-patches@cygwin.com
Subject: [PATCH 1/1] Cygwin: console: Maintain EXTENDED_FLAGS state
Date: Thu, 10 Feb 2022 10:38:08 -0500	[thread overview]
Message-ID: <20220210153808.2655-1-mhentges@mozilla.com> (raw)
In-Reply-To: <20220210170756.a2efb012fdc916e3873b1b55@nifty.ne.jp>

As well-described over in this post [1], it's possible for
the active console mode to be impossible to correctly determine.
Specifically, if ENABLE_EXTENDED_FLAGS is at any point unset,
then the flags it's associated with (ENABLE_INSERT_MODE,
ENABLE_QUICK_EDIT_MODE) will no longer be discoverable - they'll
always show up as unset, regardless of real console state.

It's not possible to work around this by setting
ENABLE_EXTENDED_FLAGS once then re-querying, because setting
ENABLE_EXTENDED_FLAGS on it's own will *disable* its related
flags.

Anyways, to avoid this case, all programs doing SetConsoleMode()
must be good community citizens and carefully maintain its state.
Unfortunately, we're accidentally stepping on this in
fhandler_console::set_input_mode().

This patch solves this by carrying forward ENABLED_EXTENDED_FLAGS
and friends in the only place where it had been ignoring it.
Since the previous behaviour of leaving all three flags unset would
essentially maintain their existing state (except for the footgun
being worked around here), *adding* the carry-over of the flags now
should not alter console behaviour.
---
 winsup/cygwin/fhandler_console.cc | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/winsup/cygwin/fhandler_console.cc b/winsup/cygwin/fhandler_console.cc
index 7a1a45bc1..b2554c3ba 100644
--- a/winsup/cygwin/fhandler_console.cc
+++ b/winsup/cygwin/fhandler_console.cc
@@ -458,16 +458,18 @@ void
 fhandler_console::set_input_mode (tty::cons_mode m, const termios *t,
 				  const handle_set_t *p)
 {
-  DWORD flags = 0, oflags;
+  DWORD oflags;
   WaitForSingleObject (p->input_mutex, mutex_timeout);
   GetConsoleMode (p->input_handle, &oflags);
+  DWORD flags = oflags
+      & (ENABLE_EXTENDED_FLAGS | ENABLE_INSERT_MODE | ENABLE_QUICK_EDIT_MODE);
   switch (m)
     {
     case tty::restore:
-      flags = ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT;
+      flags |= ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT;
       break;
     case tty::cygwin:
-      flags = ENABLE_WINDOW_INPUT;
+      flags |= ENABLE_WINDOW_INPUT;
       if (wincap.has_con_24bit_colors () && !con_is_legacy)
 	flags |= ENABLE_VIRTUAL_TERMINAL_INPUT;
       else
-- 
2.35.1


  reply	other threads:[~2022-02-10 15:38 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-10  0:09 Mitchell Hentges
2022-02-10  8:07 ` Takashi Yano
2022-02-10 15:38   ` Mitchell Hentges [this message]
2022-02-10 15:40     ` Mitchell Hentges
2022-02-11  0:12       ` Takashi Yano
2022-02-11  5:10         ` Mitchell Hentges

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=20220210153808.2655-1-mhentges@mozilla.com \
    --to=mhentges@mozilla.com \
    --cc=cygwin-patches@cygwin.com \
    /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).