From 8bf4902569869133affd91bdcb71b74656cfc36e Mon Sep 17 00:00:00 2001 From: Jilles Tjoelker Date: Wed, 3 Jun 2026 22:42:01 +0200 Subject: [PATCH] diff: Correct fd 0 case on pipe After git commit c8d40bf8ecc60cc15e3904410db62065ea681fdc, if fd 0 was not open, it is left with CLOEXEC set and therefore fails. This is an unlikely situation, but fixing it reduces the size of the code (by using posix_spawn_file_actions_adddup2's special case if the two file descriptor numbers are the same). At the same time, check the error code from posix_spawn_file_actions_adddup2. Reviewed by: bapt Differential Revision: https://reviews.freebsd.org/D56910 --- usr.bin/diff/pr.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/usr.bin/diff/pr.c b/usr.bin/diff/pr.c index e8a4162d8b1..57cbb6a56ce 100644 --- a/usr.bin/diff/pr.c +++ b/usr.bin/diff/pr.c @@ -71,8 +71,9 @@ start_pr(char *file1, char *file2) posix_spawnattr_setprocdescp_np(&sa, &pr->procd, 0); - if (pfd[0] != STDIN_FILENO) - posix_spawn_file_actions_adddup2(&fa, pfd[0], STDIN_FILENO); + error = posix_spawn_file_actions_adddup2(&fa, pfd[0], STDIN_FILENO); + if (error != 0) + errc(2, error, "posix_spawn_file_actions_adddup2"); char *argv[] = { __DECONST(char *, _PATH_PR), __DECONST(char *, "-h"), header, NULL };