changeset 15776:1544d00cbf34

fdopen: Support for MSVC 9. * m4/fdopen.m4 (gl_FUNC_FDOPEN): Set REPLACE_FDOPEN also if HAVE_MSVC_INVALID_PARAMETER_HANDLER is 1. * lib/fdopen.c: Include msvc-inval.h. (fdopen_nothrow): New function. (rpl_fdopen): Use it. * modules/fdopen (Depends-on): Add msvc-inval. * modules/fclose-tests (Depends-on): Add fdopen. * modules/fflush-tests (Depends-on): Likewise. * modules/fgetc-tests (Depends-on): Likewise. * modules/fputc-tests (Depends-on): Likewise. * modules/fread-tests (Depends-on): Likewise. * modules/freopen-tests (Depends-on): Likewise. * modules/fseeko-tests (Depends-on): Likewise. * modules/ftello-tests (Depends-on): Likewise. * modules/fwrite-tests (Depends-on): Likewise. * doc/posix-functions/fdopen.texi: Mention the problem on MSVC.
author Bruno Haible <bruno@clisp.org>
date Sat, 24 Sep 2011 18:08:50 +0200
parents 89e7b0355fd3
children 014192d3afa0
files ChangeLog doc/posix-functions/fdopen.texi lib/fdopen.c m4/fdopen.m4 modules/fclose-tests modules/fdopen modules/fflush-tests modules/fgetc-tests modules/fputc-tests modules/fread-tests modules/freopen-tests modules/fseeko-tests modules/ftello-tests modules/fwrite-tests
diffstat 14 files changed, 82 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,23 @@
+2011-09-24  Bruno Haible  <bruno@clisp.org>
+
+	fdopen: Support for MSVC 9.
+	* m4/fdopen.m4 (gl_FUNC_FDOPEN): Set REPLACE_FDOPEN also if
+	HAVE_MSVC_INVALID_PARAMETER_HANDLER is 1.
+	* lib/fdopen.c: Include msvc-inval.h.
+	(fdopen_nothrow): New function.
+	(rpl_fdopen): Use it.
+	* modules/fdopen (Depends-on): Add msvc-inval.
+	* modules/fclose-tests (Depends-on): Add fdopen.
+	* modules/fflush-tests (Depends-on): Likewise.
+	* modules/fgetc-tests (Depends-on): Likewise.
+	* modules/fputc-tests (Depends-on): Likewise.
+	* modules/fread-tests (Depends-on): Likewise.
+	* modules/freopen-tests (Depends-on): Likewise.
+	* modules/fseeko-tests (Depends-on): Likewise.
+	* modules/ftello-tests (Depends-on): Likewise.
+	* modules/fwrite-tests  (Depends-on): Likewise.
+	* doc/posix-functions/fdopen.texi: Mention the problem on MSVC.
+
 2011-09-24  Bruno Haible  <bruno@clisp.org>
 
 	fgetc, fputc, fread, fwrite tests: Avoid compilation error on MSVC.
--- a/doc/posix-functions/fdopen.texi
+++ b/doc/posix-functions/fdopen.texi
@@ -9,6 +9,9 @@
 Portability problems fixed by Gnulib:
 @itemize
 @item
+This function crashes when invoked with invalid arguments on some platforms:
+MSVC 9.
+@item
 On Windows platforms (excluding Cygwin), this function does not set @code{errno}
 upon failure.
 @end itemize
--- a/lib/fdopen.c
+++ b/lib/fdopen.c
@@ -21,8 +21,34 @@
 
 #include <errno.h>
 
+#if HAVE_MSVC_INVALID_PARAMETER_HANDLER
+# include "msvc-inval.h"
+#endif
+
 #undef fdopen
 
+#if HAVE_MSVC_INVALID_PARAMETER_HANDLER
+static FILE *
+fdopen_nothrow (int fd, const char *mode)
+{
+  FILE *result;
+
+  TRY_MSVC_INVAL
+    {
+      result = fdopen (fd, mode);
+    }
+  CATCH_MSVC_INVAL
+    {
+      result = NULL;
+    }
+  DONE_MSVC_INVAL;
+
+  return result;
+}
+#else
+# define fdopen_nothrow fdopen
+#endif
+
 FILE *
 rpl_fdopen (int fd, const char *mode)
 {
@@ -30,7 +56,7 @@
   FILE *fp;
 
   errno = 0;
-  fp = fdopen (fd, mode);
+  fp = fdopen_nothrow (fd, mode);
   if (fp == NULL)
     {
       if (errno == 0)
--- a/m4/fdopen.m4
+++ b/m4/fdopen.m4
@@ -1,4 +1,4 @@
-# fdopen.m4 serial 1
+# fdopen.m4 serial 2
 dnl Copyright (C) 2011 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -8,12 +8,15 @@
 [
   AC_REQUIRE([gl_STDIO_H_DEFAULTS])
   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
-
-  dnl Test whether fdopen() sets errno when it fails due to a bad fd argument.
-  AC_CACHE_CHECK([whether fdopen sets errno], [gl_cv_func_fdopen_works],
-    [
-      AC_RUN_IFELSE(
-        [AC_LANG_SOURCE([[
+  AC_REQUIRE([gl_MSVC_INVAL])
+  if test $HAVE_MSVC_INVALID_PARAMETER_HANDLER = 1; then
+    REPLACE_FDOPEN=1
+  else
+    dnl Test whether fdopen() sets errno when it fails due to a bad fd argument.
+    AC_CACHE_CHECK([whether fdopen sets errno], [gl_cv_func_fdopen_works],
+      [
+        AC_RUN_IFELSE(
+          [AC_LANG_SOURCE([[
 #include <stdio.h>
 #include <errno.h>
 int
@@ -28,17 +31,18 @@
     return 2;
   return 0;
 }]])],
-        [gl_cv_func_fdopen_works=yes],
-        [gl_cv_func_fdopen_works=no],
-        [case "$host_os" in
-           mingw*) gl_cv_func_fdopen_works="guessing no" ;;
-           *)      gl_cv_func_fdopen_works="guessing yes" ;;
-         esac
-        ])
-    ])
-  case "$gl_cv_func_fdopen_works" in
-    *no) REPLACE_FDOPEN=1 ;;
-  esac
+          [gl_cv_func_fdopen_works=yes],
+          [gl_cv_func_fdopen_works=no],
+          [case "$host_os" in
+             mingw*) gl_cv_func_fdopen_works="guessing no" ;;
+             *)      gl_cv_func_fdopen_works="guessing yes" ;;
+           esac
+          ])
+      ])
+    case "$gl_cv_func_fdopen_works" in
+      *no) REPLACE_FDOPEN=1 ;;
+    esac
+  fi
 ])
 
 dnl Prerequisites of lib/fdopen.c.
--- a/modules/fclose-tests
+++ b/modules/fclose-tests
@@ -2,6 +2,7 @@
 tests/test-fclose.c
 
 Depends-on:
+fdopen
 
 configure.ac:
 
--- a/modules/fdopen
+++ b/modules/fdopen
@@ -7,6 +7,7 @@
 
 Depends-on:
 stdio
+msvc-inval      [test $REPLACE_FDOPEN = 1]
 
 configure.ac:
 gl_FUNC_FDOPEN
--- a/modules/fflush-tests
+++ b/modules/fflush-tests
@@ -7,6 +7,7 @@
 
 Depends-on:
 binary-io
+fdopen
 fseeko
 
 configure.ac:
--- a/modules/fgetc-tests
+++ b/modules/fgetc-tests
@@ -5,6 +5,7 @@
 
 Depends-on:
 unistd
+fdopen
 
 configure.ac:
 
--- a/modules/fputc-tests
+++ b/modules/fputc-tests
@@ -5,6 +5,7 @@
 
 Depends-on:
 unistd
+fdopen
 
 configure.ac:
 
--- a/modules/fread-tests
+++ b/modules/fread-tests
@@ -5,6 +5,7 @@
 
 Depends-on:
 unistd
+fdopen
 
 configure.ac:
 
--- a/modules/freopen-tests
+++ b/modules/freopen-tests
@@ -4,6 +4,7 @@
 tests/macros.h
 
 Depends-on:
+fdopen
 
 configure.ac:
 
--- a/modules/fseeko-tests
+++ b/modules/fseeko-tests
@@ -11,6 +11,7 @@
 m4/ungetc.m4
 
 Depends-on:
+fdopen
 
 configure.ac:
 gl_FUNC_UNGETC_WORKS
--- a/modules/ftello-tests
+++ b/modules/ftello-tests
@@ -11,6 +11,7 @@
 
 Depends-on:
 binary-io
+fdopen
 
 configure.ac:
 gl_FUNC_UNGETC_WORKS
--- a/modules/fwrite-tests
+++ b/modules/fwrite-tests
@@ -5,6 +5,7 @@
 
 Depends-on:
 unistd
+fdopen
 
 configure.ac: