From 54c3ecbf787d9d9d5646842802c3f1ea7855834f Mon Sep 17 00:00:00 2001
From: Jeremy Drake <github@jdrake.com>
Date: Mon, 21 Jun 2021 12:27:02 -0700
Subject: [PATCH 42/N] Fix 64 vs 32-bit type confusion in swprintf.

kill_via_console_helper: use %u instead of %ld for DWORD.
It appears that cygwin's swprintf is acting like LP64 instead of Windows
LLP64, and expecting a 64-bit long.  DWORDs are 32-bit unsigned.

Add check to avoid closing the handle from the input paramter.  It may
well be that the caller still intends to use it.

Minor fixes for Ctrl-C handling code.

From my post-merge review at https://github.com/msys2/msys2-runtime/pull/31#pullrequestreview-688771861
---
 winsup/cygwin/include/cygwin/exit_process.h | 19 ++++++++++++-------
 1 file changed, 12 insertions(+), 7 deletions(-)

diff --git a/winsup/cygwin/include/cygwin/exit_process.h b/winsup/cygwin/include/cygwin/exit_process.h
index 19b8e60..5f926c9 100644
--- a/winsup/cygwin/include/cygwin/exit_process.h
+++ b/winsup/cygwin/include/cygwin/exit_process.h
@@ -75,13 +75,14 @@ kill_via_console_helper (HANDLE process, wchar_t *function_name, int exit_code,
 
   STARTUPINFOW si = {};
   PROCESS_INFORMATION pi;
-  size_t len = wcslen (wbuf) + wcslen (function_name) + 3 /* exit code */
-               + 1 /* space */ + 10 /* process ID, i.e. DWORD */ + 1 /* NUL */;
+  size_t len = wcslen (wbuf) + 1 /* space */ + wcslen (function_name)
+               + 1 /* space */ + 3 /* exit code */ + 1 /* space */
+               + 10 /* process ID, i.e. DWORD */ + 1 /* NUL */;
   WCHAR cmd[len + 1];
   WCHAR title[] = L"cygwin-console-helper";
   DWORD process_exit;
 
-  swprintf (cmd, len + 1, L"%S %S %d %ld", wbuf, function_name, exit_code,
+  swprintf (cmd, len + 1, L"%S %S %d %u", wbuf, function_name, exit_code,
             pid);
 
   si.cb = sizeof (si);
@@ -194,10 +195,10 @@ exit_process_tree (HANDLE main_process, int exit_code)
   PROCESSENTRY32 entry;
   DWORD pids[16384];
   int max_len = sizeof (pids) / sizeof (*pids), i, len, ret = 0;
-  pid_t pid = GetProcessId (main_process);
+  DWORD pid = GetProcessId (main_process);
   int signo = exit_code & 0x7f;
 
-  pids[0] = (DWORD)pid;
+  pids[0] = pid;
   len = 1;
 
   /*
@@ -249,6 +250,8 @@ exit_process_tree (HANDLE main_process, int exit_code)
         break;
     }
 
+  CloseHandle (snapshot);
+
   for (i = len - 1; i >= 0; i--)
     {
       HANDLE process;
@@ -262,7 +265,9 @@ exit_process_tree (HANDLE main_process, int exit_code)
                   | PROCESS_VM_OPERATION | PROCESS_VM_WRITE | PROCESS_VM_READ,
               FALSE, pids[i]);
           if (!process)
-            process = OpenProcess (PROCESS_TERMINATE, FALSE, pids[i]);
+            process = OpenProcess (
+                PROCESS_QUERY_LIMITED_INFORMATION | PROCESS_TERMINATE,
+                FALSE, pids[i]);
         }
       DWORD code;
 
@@ -272,7 +277,7 @@ exit_process_tree (HANDLE main_process, int exit_code)
           if (!exit_process (process, exit_code))
             ret = -1;
         }
-      if (process)
+      if (process && process != main_process)
         CloseHandle (process);
     }
 
-- 
2.32.0.windows.1

