@@ -702,24 +702,43 @@ fhandler_termios::fstat (struct stat *buf)
702702}
703703
704704static bool
705- is_console_app (const WCHAR *filename )
705+ is_console_app (path_conv &pc )
706706{
707+ tmp_pathbuf tp;
708+ WCHAR *native_path = tp.w_get ();
709+ pc.get_wide_win32_path (native_path);
710+
711+ wchar_t *e = wcsrchr (native_path, L' .' );
712+ if (e && (wcscasecmp (e, L" .bat" ) == 0 || wcscasecmp (e, L" .cmd" ) == 0 ))
713+ return true ;
714+
715+ if (pc.is_app_execution_alias ())
716+ {
717+ UNICODE_STRING upath;
718+ RtlInitUnicodeString (&upath, native_path);
719+ path_conv target (&upath, PC_SYM_FOLLOW);
720+ target.get_wide_win32_path (native_path);
721+ }
722+
707723 HANDLE h;
708- h = CreateFileW (filename , GENERIC_READ, FILE_SHARE_READ,
724+ h = CreateFileW (native_path , GENERIC_READ, FILE_SHARE_READ,
709725 NULL , OPEN_EXISTING, 0 , NULL );
726+ if (h == INVALID_HANDLE_VALUE)
727+ return true ;
710728 char buf[1024 ];
711729 DWORD n;
712- ReadFile (h, buf, sizeof (buf), &n, 0 );
730+ BOOL res = ReadFile (h, buf, sizeof (buf), &n, 0 );
713731 CloseHandle (h);
732+ if (!res)
733+ return true ;
714734 /* The offset of Subsystem is the same for both IMAGE_NT_HEADERS32 and
715735 IMAGE_NT_HEADERS64, so only IMAGE_NT_HEADERS32 is used here. */
716736 IMAGE_NT_HEADERS32 *p = (IMAGE_NT_HEADERS32 *) memmem (buf, n, " PE\0\0 " , 4 );
717737 if (p && (char *) &p->OptionalHeader .DllCharacteristics <= buf + n)
718738 return p->OptionalHeader .Subsystem == IMAGE_SUBSYSTEM_WINDOWS_CUI;
719- wchar_t *e = wcsrchr (filename, L' .' );
720- if (e && (wcscasecmp (e, L" .bat" ) == 0 || wcscasecmp (e, L" .cmd" ) == 0 ))
721- return true ;
722- return false ;
739+ /* Return true for unknown to avoid standard handles from being unset.
740+ Setting-up standard handles for GUI apps is pointless, but not unsafe. */
741+ return true ;
723742}
724743
725744int
@@ -755,7 +774,7 @@ fhandler_termios::ioctl (unsigned int cmd, void *varg)
755774
756775void
757776fhandler_termios::spawn_worker::setup (bool iscygwin, HANDLE h_stdin,
758- const WCHAR *runpath , bool nopcon,
777+ path_conv &pc , bool nopcon,
759778 bool reset_sendsig,
760779 const WCHAR *envblock)
761780{
@@ -794,7 +813,7 @@ fhandler_termios::spawn_worker::setup (bool iscygwin, HANDLE h_stdin,
794813 ptys->setup_locale ();
795814 }
796815 }
797- if (!iscygwin && ptys_primary && is_console_app (runpath ))
816+ if (!iscygwin && ptys_primary && is_console_app (pc ))
798817 {
799818 if (h_stdin == ptys_primary->get_handle_nat ())
800819 stdin_is_ptys = true ;
0 commit comments