From 9fd76414684cfc137252b4a63a015dfc29e79fdb Mon Sep 17 00:00:00 2001
From: Takashi Yano <takashi.yano@nifty.ne.jp>
Date: Fri, 2 Feb 2024 13:59:19 +0900
Subject: [PATCH 55/N] Cygwin: console: Fix exit code for non-cygwin process.

If non-cygwin process is executed in console, the exit code is not
set correctly. This is because the stub process for non-cygwin app
crashes in fhandler_console::set_disable_master_thread() due to NULL
pointer dereference. This bug was introduced by the commit:
3721a756b0d8 ("Cygwin: console: Make the console accessible from
other terminals."), that the pointer cons is accessed before fixing
when it is NULL. This patch fixes the issue.

Backported-from: aa73e11524 (Cygwin: console: Fix exit code for non-cygwin process., 2024-02-02)
Fixes: 3721a756b0d8 ("Cygwin: console: Make the console accessible from other terminals.")
Reported-by: Johannes Schindelin <Johannes.Schindelin@gmx.de>
Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
 winsup/cygwin/fhandler/console.cc | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/winsup/cygwin/fhandler/console.cc b/winsup/cygwin/fhandler/console.cc
index c9b27c9..6c485f9 100644
--- a/winsup/cygwin/fhandler/console.cc
+++ b/winsup/cygwin/fhandler/console.cc
@@ -4328,8 +4328,6 @@ fhandler_console::need_console_handler ()
 void
 fhandler_console::set_disable_master_thread (bool x, fhandler_console *cons)
 {
-  if (con.disable_master_thread == x)
-    return;
   if (cons == NULL)
     {
       if (cygheap->ctty && cygheap->ctty->get_major () == DEV_CONS_MAJOR)
@@ -4337,6 +4335,8 @@ fhandler_console::set_disable_master_thread (bool x, fhandler_console *cons)
       else
 	return;
     }
+  if (con.disable_master_thread == x)
+    return;
   cons->acquire_input_mutex (mutex_timeout);
   con.disable_master_thread = x;
   cons->release_input_mutex ();
