From fd66eccd1143a76022db99b5bb8b20a1acb14384 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 | 11 ++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index 1f85990c9b..c09ebb954e 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 3c9e167539..d1ebb0d88c 100644 --- a/lib/dup3.c +++ b/lib/dup3.c @@ -1,5 +1,5 @@ /* Copy a file descriptor, applying specific flags. - Copyright (C) 2009-2023 Free Software Foundation, Inc. + Copyright (C) 2009-2024 Free Software Foundation, Inc. This file is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as @@ -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