Skip to content

Commit 1f87881

Browse files
committed
Cygwin: is_console_app(): deal with the .bat/.cmd file extensions first
This function contains special handling of these file extensions, treating them as console applications always, even if the first 1024 bytes do not contain a PE header with the console bits set. However, Batch and Command files are never expected to have such a header, therefore opening them and reading their first bytes is a waste of time. Let's honor the best practice to deal with easy conditions that allow early returns first. Fixes: bb42852 (Cygwin: pty: Implement new pseudo console suppot., 2020-08-19) Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 554422d commit 1f87881

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

winsup/cygwin/fhandler/termios.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,9 @@ fhandler_termios::fstat (struct stat *buf)
704704
static bool
705705
is_console_app (const WCHAR *filename)
706706
{
707+
wchar_t *e = wcsrchr (filename, L'.');
708+
if (e && (wcscasecmp (e, L".bat") == 0 || wcscasecmp (e, L".cmd") == 0))
709+
return true;
707710
HANDLE h;
708711
h = CreateFileW (filename, GENERIC_READ, FILE_SHARE_READ,
709712
NULL, OPEN_EXISTING, 0, NULL);
@@ -720,9 +723,6 @@ is_console_app (const WCHAR *filename)
720723
IMAGE_NT_HEADERS32 *p = (IMAGE_NT_HEADERS32 *) memmem (buf, n, "PE\0\0", 4);
721724
if (p && (char *) &p->OptionalHeader.DllCharacteristics <= buf + n)
722725
return p->OptionalHeader.Subsystem == IMAGE_SUBSYSTEM_WINDOWS_CUI;
723-
wchar_t *e = wcsrchr (filename, L'.');
724-
if (e && (wcscasecmp (e, L".bat") == 0 || wcscasecmp (e, L".cmd") == 0))
725-
return true;
726726
/* Return true for unknown to avoid standard handles from being unset.
727727
Setting-up standard handles for GUI apps is pointless, but not unsafe. */
728728
return true;

0 commit comments

Comments
 (0)