]> Savannah Git Hosting - gnulib.git/commitdiff
dup3: Fix behavior for equal file descriptors on NetBSD.
authorCollin Funk <collin.funk1@gmail.com>
Sat, 18 May 2024 23:34:55 +0000 (16:34 -0700)
committerBruno Haible <bruno@clisp.org>
Wed, 22 May 2024 08:59:21 +0000 (10:59 +0200)
* 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
lib/dup3.c

index 1f85990c9b5d30260c52a7388cfd81ce6ec3f1b4..c09ebb954e7023c2903bc8a69c185b139ff702c2 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2024-05-18  Collin Funk  <collin.funk1@gmail.com>
+
+       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  <bruno@clisp.org>
 
        unistd: Fix compilation error with MSVC in C++ mode.
index 3c9e1675394b206de6546de403852c90f8321a15..d1ebb0d88ca758f9804f8f752e5228c93cfe068f 100644 (file)
@@ -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);