Skip to content

Commit e78e3c8

Browse files
committed
squash! mingw: spawned processes need to inherit only standard handles
We need to make sure that the list of handles to inherit does not contain duplicates; Otherwise CreateProcessW() would fail with ERROR_INVALID_ARGUMENT. While at it, stop setting errno to ENOENT unless it really is the correct value. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 3016f0c commit e78e3c8

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

compat/mingw.c

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1658,6 +1658,21 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen
16581658
stdhandles[stdhandles_count++] = si.StartupInfo.hStdOutput;
16591659
if (si.StartupInfo.hStdError != INVALID_HANDLE_VALUE)
16601660
stdhandles[stdhandles_count++] = si.StartupInfo.hStdError;
1661+
1662+
/* The list of handles cannot contain duplicates */
1663+
if (stdhandles_count == 3) {
1664+
if (stdhandles[2] == stdhandles[0])
1665+
stdhandles_count =
1666+
stdhandles[2] == stdhandles[1] ? 1 : 2;
1667+
else if (stdhandles[2] == stdhandles[1])
1668+
stdhandles_count = 2;
1669+
else if (stdhandles[1] == stdhandles[0]) {
1670+
stdhandles_count = 2;
1671+
stdhandles[1] = stdhandles[2];
1672+
}
1673+
} else if (stdhandles_count == 2 && stdhandles[1] == stdhandles[0])
1674+
stdhandles_count = 1;
1675+
16611676
if (stdhandles_count)
16621677
si.StartupInfo.dwFlags |= STARTF_USESTDHANDLES;
16631678

@@ -1737,6 +1752,8 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen
17371752
stdhandles_count ? TRUE : FALSE,
17381753
flags, wenvblk, dir ? wdir : NULL,
17391754
&si.StartupInfo, &pi);
1755+
if (!ret)
1756+
errno = err_win_to_posix(GetLastError());
17401757

17411758
if (si.lpAttributeList)
17421759
DeleteProcThreadAttributeList(si.lpAttributeList);
@@ -1746,10 +1763,9 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen
17461763
free(wenvblk);
17471764
free(wargs);
17481765

1749-
if (!ret) {
1750-
errno = ENOENT;
1766+
if (!ret)
17511767
return -1;
1752-
}
1768+
17531769
CloseHandle(pi.hThread);
17541770

17551771
/*

0 commit comments

Comments
 (0)