On Haiku calling dup3 with the same file descriptor as both arguments
suceeds.

The same happened on NetBSD until a year ago, so it is a simple
#if defined change.

Also submitted a bug report for Haiku [1].

Collin

[1] https://dev.haiku-os.org/ticket/19476

>From ed91d9b1c2f697a39116da46b553dfc89474f490 Mon Sep 17 00:00:00 2001
From: Collin Funk <collin.fu...@gmail.com>
Date: Wed, 12 Mar 2025 18:55:26 -0700
Subject: [PATCH] dup3: Fix behavior for equal file descriptors on Haiku.

* lib/dup3.c (dup3) [__HAIKU__]: Set errno to EINVAL and return -1 if
both file descriptors are equal.
* doc/posix-functions/dup3.texi: Document the Haiku bug.
---
 ChangeLog                     | 7 +++++++
 doc/posix-functions/dup3.texi | 3 ++-
 lib/dup3.c                    | 6 ++++--
 3 files changed, 13 insertions(+), 3 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 0d363007b7..55bbf3f5e4 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2025-03-12  Collin Funk  <collin.fu...@gmail.com>
+
+	dup3: Fix behavior for equal file descriptors on Haiku.
+	* lib/dup3.c (dup3) [__HAIKU__]: Set errno to EINVAL and return -1 if
+	both file descriptors are equal.
+	* doc/posix-functions/dup3.texi: Document the Haiku bug.
+
 2025-03-12  Bruno Haible  <br...@clisp.org>
 
 	fcntl-h: Fix GNULIB_defined_O_NONBLOCK on Haiku (regr. 2025-02-16).
diff --git a/doc/posix-functions/dup3.texi b/doc/posix-functions/dup3.texi
index e7e4a507d0..3e9bd79fb0 100644
--- a/doc/posix-functions/dup3.texi
+++ b/doc/posix-functions/dup3.texi
@@ -22,7 +22,8 @@ @node dup3
 @item
 This function mistakenly succeeds when given two equal file descriptors on some platforms:
 @c https://gnats.netbsd.org/cgi-bin/query-pr-single.pl?number=58266
-NetBSD 10.0.
+@c https://dev.haiku-os.org/ticket/19476
+NetBSD 10.0, Haiku.
 @end itemize
 
 Portability problems not fixed by Gnulib:
diff --git a/lib/dup3.c b/lib/dup3.c
index 70c55990b0..2d5d51a769 100644
--- a/lib/dup3.c
+++ b/lib/dup3.c
@@ -46,8 +46,10 @@ dup3 (int oldfd, int newfd, int flags)
           {
             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__
+               expected to fail with error EINVAL.
+
+               Likewise on Haiku.  */
+# if defined __NetBSD__ || defined __HAIKU__
             if (newfd == oldfd)
               {
                 errno = EINVAL;
-- 
2.48.1

Reply via email to