Skip to content

Commit

Permalink
Fix 64 vs 32-bit type confusion in swprintf.
Browse files Browse the repository at this point in the history
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 #31 (review)
  • Loading branch information
jeremyd2019 authored and lazka committed Oct 30, 2021
1 parent 7bc2df9 commit 43d8fe9
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions winsup/cygwin/include/cygwin/exit_process.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;

/*
Expand Down Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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);
}

Expand Down

0 comments on commit 43d8fe9

Please sign in to comment.