[newlib-cygwin/cygwin-3_6-branch] Cygwin: is_console_app(): do handle errors
Takashi Yano
tyan0@sourceware.org
Fri Dec 19 18:28:43 GMT 2025
https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=c0c6185bbd4301e1cd8e2c3edebd66fb8109e349
commit c0c6185bbd4301e1cd8e2c3edebd66fb8109e349
Author: Johannes Schindelin <johannes.schindelin@gmx.de>
Date: Mon Dec 15 17:06:33 2025 +0000
Cygwin: is_console_app(): do handle errors
When that function was introduced in bb4285206207 (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 32d6a6cb5f1e
(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: bb4285206207 (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)
Diff:
---
winsup/cygwin/fhandler/termios.cc | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)
diff --git a/winsup/cygwin/fhandler/termios.cc b/winsup/cygwin/fhandler/termios.cc
index 645aa1afa..61d01e931 100644
--- a/winsup/cygwin/fhandler/termios.cc
+++ b/winsup/cygwin/fhandler/termios.cc
@@ -707,10 +707,14 @@ is_console_app (const WCHAR *filename)
HANDLE h;
h = CreateFileW (filename, GENERIC_READ, FILE_SHARE_READ,
NULL, OPEN_EXISTING, 0, NULL);
+ if (h == INVALID_HANDLE_VALUE)
+ return true;
char buf[1024];
DWORD n;
- ReadFile (h, buf, sizeof (buf), &n, 0);
+ BOOL res = ReadFile (h, buf, sizeof (buf), &n, 0);
CloseHandle (h);
+ if (!res)
+ return true;
/* The offset of Subsystem is the same for both IMAGE_NT_HEADERS32 and
IMAGE_NT_HEADERS64, so only IMAGE_NT_HEADERS32 is used here. */
IMAGE_NT_HEADERS32 *p = (IMAGE_NT_HEADERS32 *) memmem (buf, n, "PE\0\0", 4);
More information about the Cygwin-cvs
mailing list