bpo-32844: Fix a subprocess misredirection of a low fd (GH5689)

bpo-32844: subprocess: Fix a potential misredirection of a low fd to stderr.

When redirecting, subprocess attempts to achieve the following state:
each fd to be redirected to is less than or equal to the fd
it is redirected from, which is necessary because redirection
occurs in the ascending order of destination descriptors.
It fails to do so in a couple of corner cases,
for example, if 1 is redirected to 2 and 0 is closed in the parent.
This commit is contained in:
Alexey Izbyshev 2018-03-26 22:49:35 +03:00 committed by Gregory P. Smith
parent de7a2f04d6
commit 0e7144b064
3 changed files with 53 additions and 1 deletions

View file

@ -424,7 +424,7 @@ child_exec(char *const exec_array[],
either 0, 1 or 2, it is possible that it is overwritten (#12607). */
if (c2pwrite == 0)
POSIX_CALL(c2pwrite = dup(c2pwrite));
if (errwrite == 0 || errwrite == 1)
while (errwrite == 0 || errwrite == 1)
POSIX_CALL(errwrite = dup(errwrite));
/* Dup fds for child.