Skip to content

Commit f5e10c4

Browse files
committed
cbits/fork-exec: Don't dup2 identical fds
Darwin violates POSIX by making `dup2(x,x)`, which should be a no-op, error. Consequently, we must take care not to `dup2` in such cases. We had already made this change in the `posix_spawnp` codepath but I had assumed that this *only* affected `posix_spawnp`, not the `dup2` system call itself.
1 parent 240f0c4 commit f5e10c4

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

cbits/posix/fork_exec.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,13 @@ setup_std_handle_fork(int fd,
6969
return 0;
7070

7171
case STD_HANDLE_USE_FD:
72-
if (dup2(b->use_fd, fd) == -1) {
73-
child_failed(pipe, "dup2");
72+
// N.B. POSIX specifies that dup2(x,x) should be a no-op, but
73+
// naturally Apple ignores this and rather fails in posix_spawn on Big
74+
// Sur.
75+
if (b->use_fd != fd) {
76+
if (dup2(b->use_fd, fd) == -1) {
77+
child_failed(pipe, "dup2");
78+
}
7479
}
7580
return 0;
7681

0 commit comments

Comments
 (0)