Skip to content

Commit ef6d451

Browse files
committed
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 <[email protected]>
2 parents dc364ab + 8b533f0 commit ef6d451

File tree

2 files changed

+42
-6
lines changed

2 files changed

+42
-6
lines changed

compat/mingw.c

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1750,14 +1750,40 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen
17501750
* handle inheritance. This is still better than failing to create
17511751
* processes.
17521752
*/
1753-
if (!ret && GetLastError() == ERROR_NO_SYSTEM_RESOURCES &&
1754-
restrict_handle_inheritance && stdhandles_count) {
1753+
if (!ret && restrict_handle_inheritance && stdhandles_count) {
1754+
DWORD err = GetLastError();
1755+
if (err != ERROR_NO_SYSTEM_RESOURCES &&
1756+
!getenv("SUPPRESS_HANDLE_INHERITANCE_WARNING")) {
1757+
struct strbuf buf = STRBUF_INIT;
1758+
DWORD fl;
1759+
int i;
1760+
1761+
setenv("SUPPRESS_HANDLE_INHERITANCE_WARNING", "1", 1);
1762+
1763+
for (i = 0; i < stdhandles_count; i++) {
1764+
HANDLE h = stdhandles[i];
1765+
strbuf_addf(&buf, "handle #%d: %p (type %lx, "
1766+
"handle info (%d) %lx\n", i, h,
1767+
GetFileType(h),
1768+
GetHandleInformation(h, &fl),
1769+
fl);
1770+
}
1771+
strbuf_addstr(&buf, "\nThis is a bug; please report it "
1772+
"at\nhttps://github.com/git-for-windows/"
1773+
"git/issues/new\n\n"
1774+
"To suppress this warning, please set "
1775+
"the environment variable\n\n"
1776+
"\tSUPPRESS_HANDLE_INHERITANCE_WARNING=1"
1777+
"\n");
1778+
warning("failed to restrict file handles (%ld)\n\n%s",
1779+
err, buf.buf);
1780+
strbuf_release(&buf);
1781+
}
17551782
restrict_handle_inheritance = 0;
17561783
flags &= ~EXTENDED_STARTUPINFO_PRESENT;
17571784
ret = CreateProcessW(*wcmd ? wcmd : NULL, wargs, NULL, NULL,
1758-
stdhandles_count ? TRUE : FALSE,
1759-
flags, wenvblk, dir ? wdir : NULL,
1760-
&si.StartupInfo, &pi);
1785+
TRUE, flags, wenvblk, dir ? wdir : NULL,
1786+
&si.StartupInfo, &pi);
17611787
}
17621788

17631789
if (!ret)

compat/winansi.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -660,10 +660,20 @@ void winansi_init(void)
660660
*/
661661
HANDLE winansi_get_osfhandle(int fd)
662662
{
663+
HANDLE ret;
664+
663665
if (fd == 1 && (fd_is_interactive[1] & FD_SWAPPED))
664666
return hconsole1;
665667
if (fd == 2 && (fd_is_interactive[2] & FD_SWAPPED))
666668
return hconsole2;
667669

668-
return (HANDLE)_get_osfhandle(fd);
670+
ret = (HANDLE)_get_osfhandle(fd);
671+
672+
/*
673+
* There are obviously circumstances under which _get_osfhandle()
674+
* returns (HANDLE)-2. This is not documented anywhere, but that is so
675+
* clearly an invalid handle value that we can just work around this
676+
* and return the correct value for invalid handles.
677+
*/
678+
return ret == (HANDLE)-2 ? INVALID_HANDLE_VALUE : ret;
669679
}

0 commit comments

Comments
 (0)