According to Simon Josefsson on 1/8/2010 2:36 AM:
> I got this on MinGW:
> 
> test-dup2.c:140: assertion failed
> FAIL: test-dup2.exe

I wish mingw had a better version number reporting scheme.  I can't
reproduce the failure when I cross-compile from cygwin to mingw, then run
the program natively on Windows (that is, using the mingw libraries that
ship with cygwin 1.7.1), but I'll take your word for it.  Maybe this is a
bug in wine?

>   ASSERT (dup2 (fd, -2) == -1);
> 
> Debugging this, it seems MinGW dup2 returns -2 in this situation.  Any
> ideas?  Is -2 a valid file descriptor on MinGW for some reason?

-2 is not a valid fd, but it does violate POSIX for a proper return value.
 At least it's pretty easy to work around.  I'll push this:

-- 
Don't work too hard, make some time for fun as well!

Eric Blake             e...@byu.net
From 47723c9e1a8568d455a722421f5250a22197ac70 Mon Sep 17 00:00:00 2001
From: Eric Blake <e...@byu.net>
Date: Fri, 8 Jan 2010 08:17:00 -0700
Subject: [PATCH] dup2: work around mingw bug

dup2 (fd, -2) returned -2 instead of the proper -1.

* lib/dup2.c (rpl_dup2): Sanitize return value on mingw.
Reported by Simon Josefsson.

Signed-off-by: Eric Blake <e...@byu.net>
---
 ChangeLog  |    6 ++++++
 lib/dup2.c |    7 +++++++
 2 files changed, 13 insertions(+), 0 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 0363a5b..e9be61a 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2010-01-08  Eric Blake  <e...@byu.net>
+
+       dup2: work around mingw bug
+       * lib/dup2.c (rpl_dup2): Sanitize return value on mingw.
+       Reported by Simon Josefsson.
+
 2010-01-07  Eric Blake  <e...@byu.net>

        maint.mk: allow packages to add sc_makefile_check exceptions
diff --git a/lib/dup2.c b/lib/dup2.c
index ef581a7..9b6a8f6 100644
--- a/lib/dup2.c
+++ b/lib/dup2.c
@@ -52,6 +52,13 @@ rpl_dup2 (int fd, int desired_fd)
         }
       return fd;
     }
+  /* Some mingw versions also return the wrong value if desired_fd is
+     negative but not -1.  */
+  if (desired_fd < 0)
+    {
+      errno = EBADF;
+      return -1;
+    }
 # endif
   result = dup2 (fd, desired_fd);
 # ifdef __linux__
-- 
1.6.4.2

Attachment: signature.asc
Description: OpenPGP digital signature

Reply via email to