]> Savannah Git Hosting - gnulib.git/commitdiff
dup2: Support for MSVC.
authorBruno Haible <bruno@clisp.org>
Sun, 18 Sep 2011 08:34:24 +0000 (10:34 +0200)
committerBruno Haible <bruno@clisp.org>
Sun, 18 Sep 2011 08:34:24 +0000 (10:34 +0200)
* lib/dup2.c: Include msvc-inval.h.
(rpl_dup2): Handle invalid parameter notifications during dup2 and
_get_osfhandle calls.
* modules/dup2 (Depends-on): Add msvc-inval.
* doc/posix-functions/dup2.texi: Mention problem on MSVC.

ChangeLog
doc/posix-functions/dup2.texi
lib/dup2.c
modules/dup2

index ce725eb6e2890cd9c7da2ce0a81a71a0c711aa70..bd98467e63313a7e74fde9bbc289abe94ae66901 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,12 @@
 2011-09-18  Bruno Haible  <bruno@clisp.org>
 
+       dup2: Support for MSVC.
+       * lib/dup2.c: Include msvc-inval.h.
+       (rpl_dup2): Handle invalid parameter notifications during dup2 and
+       _get_osfhandle calls.
+       * modules/dup2 (Depends-on): Add msvc-inval.
+       * doc/posix-functions/dup2.texi: Mention problem on MSVC.
+
        New module 'msvc-inval'.
        * lib/msvc-inval.h: New file.
        * lib/msvc-inval.c: New file.
index 8ba64bda7440d8f75613ca05f4271fd91bc5c2f1..012bb7f0e091df6a611d99dd73a9c85e6fccd6a4 100644 (file)
@@ -16,6 +16,10 @@ mingw, MSVC 9.
 This function can hang when duplicating an fd to itself on some platforms:
 mingw, MSVC 9.
 
+@item
+This function crashes when invoked with invalid arguments on some platforms:
+MSVC 9.
+
 @item
 This function resets the @code{FD_CLOEXEC} flag when duplicating an fd
 to itself on some platforms:
index e00dc7b2e3c5f2789bc6a578a5f45be67871a2ef..09ae49fb3ff713529629a551183b6204d1b713c8 100644 (file)
@@ -31,6 +31,8 @@
 # include <windows.h>
 #endif
 
+#include "msvc-inval.h"
+
 #if HAVE_DUP2
 
 # undef dup2
@@ -45,7 +47,19 @@ rpl_dup2 (int fd, int desired_fd)
      future dup2 calls will hang.  */
   if (fd == desired_fd)
     {
-      if ((HANDLE) _get_osfhandle (fd) == INVALID_HANDLE_VALUE)
+      HANDLE handle;
+
+      TRY_MSVC_INVAL
+        {
+          handle = (HANDLE) _get_osfhandle (fd);
+        }
+      CATCH_MSVC_INVAL
+        {
+          handle = INVALID_HANDLE_VALUE;
+        }
+      DONE_MSVC_INVAL
+
+      if (handle == INVALID_HANDLE_VALUE)
         {
           errno = EBADF;
           return -1;
@@ -64,7 +78,18 @@ rpl_dup2 (int fd, int desired_fd)
   if (fd == desired_fd)
     return fcntl (fd, F_GETFL) == -1 ? -1 : fd;
 # endif
-  result = dup2 (fd, desired_fd);
+
+  TRY_MSVC_INVAL
+    {
+      result = dup2 (fd, desired_fd);
+    }
+  CATCH_MSVC_INVAL
+    {
+      result = -1;
+      errno = EBADF;
+    }
+  DONE_MSVC_INVAL
+
 # ifdef __linux__
   /* Correct a Linux return value.
      <http://git.kernel.org/?p=linux/kernel/git/stable/linux-2.6.30.y.git;a=commitdiff;h=2b79bc4f7ebbd5af3c8b867968f9f15602d5f802>
index f9a1ee3f3789507ec3125fa599e1483afb25ce91..a94dc17162df2a6517f81ee2ebf8d3f1b35de7ad 100644 (file)
@@ -8,6 +8,7 @@ m4/dup2.m4
 Depends-on:
 unistd
 dup2-obsolete
+msvc-inval      [test $HAVE_DUP2 = 0 || test $REPLACE_DUP2 = 1]
 
 configure.ac:
 gl_FUNC_DUP2