Skip to content
Permalink
Browse files

Merge branch 'inherit-only-stdhandles-fixup'

Some more changes to fix a real bug affecting TortoiseGit users, and to
make the code a bit more robust in general.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
  • Loading branch information...
dscho committed Feb 7, 2018
2 parents dc364ab + 8b533f0 commit ef6d451bbfef86a529ebf12620289e0f15a93d8e
Showing with 42 additions and 6 deletions.
  1. +31 −5 compat/mingw.c
  2. +11 −1 compat/winansi.c
@@ -1750,14 +1750,40 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen
* handle inheritance. This is still better than failing to create
* processes.
*/
if (!ret && GetLastError() == ERROR_NO_SYSTEM_RESOURCES &&
restrict_handle_inheritance && stdhandles_count) {
if (!ret && restrict_handle_inheritance && stdhandles_count) {
DWORD err = GetLastError();
if (err != ERROR_NO_SYSTEM_RESOURCES &&
!getenv("SUPPRESS_HANDLE_INHERITANCE_WARNING")) {
struct strbuf buf = STRBUF_INIT;
DWORD fl;
int i;

setenv("SUPPRESS_HANDLE_INHERITANCE_WARNING", "1", 1);

for (i = 0; i < stdhandles_count; i++) {
HANDLE h = stdhandles[i];
strbuf_addf(&buf, "handle #%d: %p (type %lx, "
"handle info (%d) %lx\n", i, h,
GetFileType(h),
GetHandleInformation(h, &fl),
fl);
}
strbuf_addstr(&buf, "\nThis is a bug; please report it "
"at\nhttps://github.com/git-for-windows/"
"git/issues/new\n\n"
"To suppress this warning, please set "
"the environment variable\n\n"
"\tSUPPRESS_HANDLE_INHERITANCE_WARNING=1"
"\n");
warning("failed to restrict file handles (%ld)\n\n%s",
err, buf.buf);
strbuf_release(&buf);
}
restrict_handle_inheritance = 0;
flags &= ~EXTENDED_STARTUPINFO_PRESENT;
ret = CreateProcessW(*wcmd ? wcmd : NULL, wargs, NULL, NULL,
stdhandles_count ? TRUE : FALSE,
flags, wenvblk, dir ? wdir : NULL,
&si.StartupInfo, &pi);
TRUE, flags, wenvblk, dir ? wdir : NULL,
&si.StartupInfo, &pi);
}

if (!ret)
@@ -660,10 +660,20 @@ void winansi_init(void)
*/
HANDLE winansi_get_osfhandle(int fd)
{
HANDLE ret;

if (fd == 1 && (fd_is_interactive[1] & FD_SWAPPED))
return hconsole1;
if (fd == 2 && (fd_is_interactive[2] & FD_SWAPPED))
return hconsole2;

return (HANDLE)_get_osfhandle(fd);
ret = (HANDLE)_get_osfhandle(fd);

/*
* There are obviously circumstances under which _get_osfhandle()
* returns (HANDLE)-2. This is not documented anywhere, but that is so
* clearly an invalid handle value that we can just work around this
* and return the correct value for invalid handles.
*/
return ret == (HANDLE)-2 ? INVALID_HANDLE_VALUE : ret;
}

0 comments on commit ef6d451

Please sign in to comment.
You can’t perform that action at this time.