git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 2/4] mingw: work around incorrect standard handles

From
Johannes Schindelin via GitGitGadget <gitgitgadget@gmail.com>
Date
Nov 22, 2019, 14:41 UTC
Message-ID
<f68b4094a2985217a0229fd6432cece9d91b25e5.1574433665.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.670.git.git.1574433665.gitgitgadget@gmail.com>
From: Johannes Schindelin <johannes.schindelin@gmx.de>

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() seems to work fine without complaints if hStdInput set to this value.

In contrast, the upcoming code to restrict which file handles get inherited by spawned processes would result in `ERROR_INVALID_PARAMETER` when including such handle values in the list.

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 fixes https://github.com/git-for-windows/git/issues/1481
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
 compat/winansi.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
diff --git a/compat/winansi.c b/compat/winansi.c
index 54fd701cbf..c27b20a79d 100644
--- a/compat/winansi.c
+++ b/compat/winansi.c
@@ -662,10 +662,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;
 }
-- 
gitgitgadget
Previous: Johannes Schindelin via GitGitGadgetNext: Johannes Schindelin via GitGitGadget
Message 2 of 13 in “On Windows, limit which file handles are inherited by spawned child processes”
  1. 0/4 On Windows, limit which file handles are inherited by spawned child processesJohannes Schindelin via GitGitGadget, Nov 22, 2019
  2. 2/4 mingw: work around incorrect standard handlesJohannes Schindelin via GitGitGadget, Nov 22, 2019
  3. 1/4 mingw: demonstrate that all file handles are inherited by child processesJohannes Schindelin via GitGitGadget, Nov 22, 2019
  4. 4/4 mingw: restrict file handle inheritance only on Windows 7 and laterJohannes Schindelin via GitGitGadget, Nov 22, 2019
  5. 3/4 mingw: spawned processes need to inherit only standard handlesJohannes Schindelin via GitGitGadget, Nov 22, 2019
  6. Johannes SixtNov 28, 2019
  7. Johannes SchindelinNov 29, 2019
  8. Johannes SchindelinNov 29, 2019
  9. Johannes SixtNov 29, 2019
  10. Johannes SixtNov 29, 2019
  11. Johannes SchindelinNov 30, 2019
  12. Junio C HamanoNov 25, 2019
  13. Johannes SchindelinNov 25, 2019

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.