Skip to content

Commit 9cb5307

Browse files
committed
mingw: work around incorrect standard handles
For some reason, when being called via TortoiseGit the standard handles, or at least what is returned by _get_osfhandle(0) for standard input, can take on the value (HANDLE)-2 (which is not a legal value, according to the documentation). Even if this value is not documented anywhere, CreateProcess() works without complaints if hStdInput set to this value. In contrast, when our new code to restrict which file handles get inherited by spawned processes apparently uses such a value in its handle list, it does *not* work, erroring out with `ERROR_INVALID_PARAMETER`. To help this, special-case the value (HANDLE)-2 returned by _get_osfhandle() and replace it with INVALID_HANDLE_VALUE, which will hopefully let the handle inheritance restriction work even when called from TortoiseGit. This addresses git-for-windows#1481 Signed-off-by: Johannes Schindelin <[email protected]>
1 parent fdd80a8 commit 9cb5307

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

compat/winansi.c

+11-1
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,8 @@ void winansi_init(void)
662662
*/
663663
HANDLE winansi_get_osfhandle(int fd)
664664
{
665+
HANDLE ret;
666+
665667
if (debug_1481)
666668
warning("get_osf_handle for %d (fd_is_interactive %x %x): "
667669
"%p (%p %p)",
@@ -672,5 +674,13 @@ HANDLE winansi_get_osfhandle(int fd)
672674
if (fd == 2 && (fd_is_interactive[2] & FD_SWAPPED))
673675
return hconsole2;
674676

675-
return (HANDLE)_get_osfhandle(fd);
677+
ret = (HANDLE)_get_osfhandle(fd);
678+
679+
/*
680+
* There are obviously circumstances under which _get_osfhandle()
681+
* returns (HANDLE)-2. This is not documented anywhere, but that is so
682+
* clearly an invalid handle value that we can just work around this
683+
* and return the correct value for invalid handles.
684+
*/
685+
return ret == (HANDLE)-2 ? INVALID_HANDLE_VALUE : ret;
676686
}

0 commit comments

Comments
 (0)