Skip to content

Commit c0c6185

Browse files
dschotyan0
andcommitted
Cygwin: is_console_app(): do handle errors
When that function was introduced in bb42852 (Cygwin: pty: Implement new pseudo console support., 2020-08-19) (back then, it was added to `spawn.cc`, later it was moved to `fhandler/termios.cc` in 32d6a6c (Cygwin: pty, console: Encapsulate spawn.cc code related to pty/console., 2022-11-19)), it was implemented with strong assumptions that neither creating the file handle nor reading 1024 bytes from said handle could fail. This assumption, however, is incorrect. Concretely, I encountered the case where `is_console_app()` needed to open an app execution alias, failed to do so, and still tried to read from the invalid handle. Let's add some error handling to that function. Fixes: bb42852 (Cygwin: pty: Implement new pseudo console support., 2020-08-19) Co-authored-by: Takashi Yano <takashi.yano@nifty.ne.jp> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> (cherry picked from commit c9b98d2e553b5d3c08b597ec1c84b7ccf0a249fe)
1 parent 97d254c commit c0c6185

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

winsup/cygwin/fhandler/termios.cc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -707,10 +707,14 @@ is_console_app (const WCHAR *filename)
707707
HANDLE h;
708708
h = CreateFileW (filename, GENERIC_READ, FILE_SHARE_READ,
709709
NULL, OPEN_EXISTING, 0, NULL);
710+
if (h == INVALID_HANDLE_VALUE)
711+
return true;
710712
char buf[1024];
711713
DWORD n;
712-
ReadFile (h, buf, sizeof (buf), &n, 0);
714+
BOOL res = ReadFile (h, buf, sizeof (buf), &n, 0);
713715
CloseHandle (h);
716+
if (!res)
717+
return true;
714718
/* The offset of Subsystem is the same for both IMAGE_NT_HEADERS32 and
715719
IMAGE_NT_HEADERS64, so only IMAGE_NT_HEADERS32 is used here. */
716720
IMAGE_NT_HEADERS32 *p = (IMAGE_NT_HEADERS32 *) memmem (buf, n, "PE\0\0", 4);

0 commit comments

Comments
 (0)