From b285f4a1b85ea31b1aeedd92ad736abf852ab3e7 Mon Sep 17 00:00:00 2001 From: Collin Funk Date: Sat, 18 May 2024 16:34:55 -0700 Subject: [PATCH] dup3: Fix behavior for equal file descriptors on NetBSD. * lib/dup3.c (dup3) [__NetBSD__]: Check for equal file descriptors upon a successful call to dup3. If they are equal fail with errno == EINVAL. --- ChangeLog | 6 ++++++ lib/dup3.c | 9 +++++++++ 2 files changed, 15 insertions(+) diff --git a/ChangeLog b/ChangeLog index 8f78d27e8a..47a23af94e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2024-05-18 Collin Funk + + dup3: Fix behavior for equal file descriptors on NetBSD. + * lib/dup3.c (dup3) [__NetBSD__]: Check for equal file descriptors upon + a successful call to dup3. If they are equal fail with errno == EINVAL. + 2024-05-17 Bruno Haible unistd: Fix compilation error with MSVC in C++ mode. diff --git a/lib/dup3.c b/lib/dup3.c index a810d3be19..d1ebb0d88c 100644 --- a/lib/dup3.c +++ b/lib/dup3.c @@ -45,6 +45,15 @@ dup3 (int oldfd, int newfd, int flags) if (!(result < 0 && errno == ENOSYS)) { have_dup3_really = 1; + /* On NetBSD dup3 is a no-op when oldfd == newfd, but we are + expected to fail with error EINVAL. */ +# ifdef __NetBSD__ + if (newfd == oldfd) + { + errno = EINVAL; + return -1; + } +# endif # if REPLACE_FCHDIR if (0 <= result) result = _gl_register_dup (oldfd, newfd); -- 2.39.5