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

Re: [PATCH 1/1] mingw: do set `errno` correctly when trying to restrict handle inheritance

From
Johannes Sixt <j6t@kdbg.org>
Date
Nov 29, 2019, 23:02 UTC
Message-ID
<52f2fe39-6a71-a327-8aea-ba757b7eec18@kdbg.org>
In-Reply-To
<685360f735e35e837bc9ef684cbde33564c81666.1575063876.git.gitgitgadget@gmail.com>
Am 29.11.19 um 22:44 schrieb Johannes Schindelin via GitGitGadget:
Show 38 quoted lines
> From: Johannes Schindelin <johannes.schindelin@gmx.de>
> 
> In 9a780a384de (mingw: spawned processes need to inherit only standard
> handles, 2019-11-22), we taught the Windows-specific part to restrict
> which file handles are passed on to the spawned processes.
> 
> Since this logic seemed to be a bit fragile across Windows versions (we
> _still_ support Windows Vista in Git for Windows, for example), a
> fall-back was added to try spawning the process again, this time without
> restricting which file handles are to be inherited by the spawned
> process.
> 
> In the common case (i.e. when the process could not be spawned for
> reasons _other_ than the file handle inheritance), the fall-back attempt
> would still fail, of course.
> 
> Crucially, one thing we missed in that code path was to set `errno`
> appropriately.
> 
> This should have been caught by t0061.2 which expected `errno` to be
> `ENOENT` after trying to start a process for a non-existing executable,
> but `errno` was set to `ENOENT` prior to the `CreateProcessW()` call:
> while looking for the config settings for trace2, Git tries to access
> `xdg_config` and `user_config` via `access_or_die()`, and as neither of
> those config files exists when running the test case (because in Git's
> test suite, `HOME` points to the test directory), the `errno` has the
> expected value, but for the wrong reasons.
> 
> Let's fix that by making sure that `errno` is set correctly.
> 
> It would be nice if we could somehow fix t0061 to make sure that this
> does not regress again. One approach that seemed like it should work,
> but did not, was to set `errno` to 0 in the test helper that is used by
> t0061.2.
> 
> However, when `mingw_spawnvpe()` wants to see whether the file in
> question is a script, it calls `parse_interpreter()`, which in turn
> tries to `open()` the file.0/compat/mingw.c#L1134. Obviously,
Copy-and-paste garbage?
Show 33 quoted lines
> this call fails, and sets `errno` to `ENOENT`, deep inside the call
> chain started from that test helper.
> 
> Instead, we force re-set `errno` at the beginning of the function
> `mingw_spawnve_fd()`, which _should_ be safe given that callers of that
> function will want to look at `errno` if -1 was returned. And if that
> `errno` is 0 ("No error"), regression tests like t0061.2 will kick in.
> 
> Reported-by: Johannes Sixt <j6t@kdbg.org>
> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
> ---
>  compat/mingw.c | 5 ++++-
>  1 file changed, 4 insertions(+), 1 deletion(-)
> 
> diff --git a/compat/mingw.c b/compat/mingw.c
> index 2b6eca2f56..bb4eb4211a 100644
> --- a/compat/mingw.c
> +++ b/compat/mingw.c
> @@ -1423,6 +1423,9 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen
>  	const char *(*quote_arg)(const char *arg) =
>  		is_msys2_sh(*argv) ? quote_arg_msys2 : quote_arg_msvc;
>  
> +	/* Make sure to override previous errors, if any */
> +	errno = 0;
> +
>  	if (restrict_handle_inheritance < 0)
>  		restrict_handle_inheritance = core_restrict_inherited_handles;
>  	/*
> @@ -1580,8 +1583,8 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen
>  		ret = CreateProcessW(*wcmd ? wcmd : NULL, wargs, NULL, NULL,
>  				     TRUE, flags, wenvblk, dir ? wdir : NULL,
>  				     &si.StartupInfo, &pi);
> +		errno = err_win_to_posix(GetLastError());

I think this should be protected by 'if (!ret)' because err_win_to_posix() does not handle ERROR_SUCCESS and turns it into ENOSYS. It's not that bad because in the case of success we do not guarantee any value of errno anyway.

Show 6 quoted lines
>  		if (ret && buf.len) {
> -			errno = err_win_to_posix(GetLastError());
>  			warning("failed to restrict file handles (%ld)\n\n%s",
>  				err, buf.buf);
>  		}
> 
That said, this fixes the failure.
-- Hannes
Previous: Johannes Schindelin via GitGitGadgetNext: Johannes Schindelin
Message 3 of 27 in “Brown-bag fix on top of js/mingw-inherit-only-std-handles”
  1. 0/1 Brown-bag fix on top of js/mingw-inherit-only-std-handlesJohannes Schindelin via GitGitGadget, Nov 29, 2019
  2. 1/1 mingw: do set `errno` correctly when trying to restrict handle inheritanceJohannes Schindelin via GitGitGadget, Nov 29, 2019
  3. Johannes SixtNov 29, 2019
  4. Johannes SchindelinNov 30, 2019
  5. Johannes SixtNov 30, 2019
  6. 0/2 Brown-bag fix on top of js/mingw-inherit-only-std-handlesJohannes Schindelin via GitGitGadget, Nov 30, 2019
  7. 1/2 mingw: do set `errno` correctly when trying to restrict handle inheritanceJohannes Schindelin via GitGitGadget, Nov 30, 2019
  8. 2/2 mingw: translate ERROR_SUCCESS to errno = 0Johannes Schindelin via GitGitGadget, Nov 30, 2019
  9. Junio C HamanoNov 30, 2019
  10. Johannes SixtNov 30, 2019
  11. Andreas SchwabNov 30, 2019
  12. Junio C HamanoNov 30, 2019
  13. Andreas SchwabNov 30, 2019
  14. Johannes SchindelinNov 30, 2019
  15. Junio C HamanoNov 30, 2019
  16. Junio C HamanoDec 1, 2019
  17. Johannes SchindelinDec 1, 2019
  18. Junio C HamanoDec 2, 2019
  19. Johannes SchindelinDec 2, 2019
  20. Johannes SixtNov 30, 2019
  21. Junio C HamanoNov 30, 2019
  22. 0/2 Brown-bag fix on top of js/mingw-inherit-only-std-handlesJohannes Schindelin via GitGitGadget, Dec 2, 2019
  23. 2/2 mingw: translate ERROR_SUCCESS to errno = 0Johannes Schindelin via GitGitGadget, Dec 2, 2019
  24. 1/2 mingw: do set `errno` correctly when trying to restrict handle inheritanceJohannes Schindelin via GitGitGadget, Dec 2, 2019
  25. Johannes SixtDec 2, 2019
  26. Junio C HamanoDec 2, 2019
  27. Johannes SchindelinDec 3, 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.