[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
fclose on MSVC
From: |
Bruno Haible |
Subject: |
fclose on MSVC |
Date: |
Sun, 25 Sep 2011 12:10:42 +0200 |
User-agent: |
KMail/1.13.6 (Linux/2.6.37.6-0.5-desktop; KDE/4.6.0; x86_64; ; ) |
On MSVC 9, it's the code from lib/fclose.c which makes fclose(stdout) crash:
if (close (fd) < 0 && saved_errno == 0)
saved_errno = errno;
fclose (fp); /* will fail with errno = EBADF, if we did not lose a race */
Here's the fix.
2011-09-25 Bruno Haible <address@hidden>
fclose: Support for MSVC 9.
* lib/fclose.c: Include msvc-inval.h.
(fclose_nothrow): New function.
(rpl_fclose): Use it.
* modules/fclose (Depends-on): Add msvc-inval.
* doc/posix-functions/fclose.texi: Mention the problem on MSVC.
--- doc/posix-functions/fclose.texi.orig Sun Sep 25 12:04:17 2011
+++ doc/posix-functions/fclose.texi Sun Sep 25 11:57:54 2011
@@ -13,6 +13,10 @@
seekable input stream to the byte after the last one actually read:
glibc 2.13, FreeBSD.
@item
+This function crashes if the stream's file descriptor has already been
+closed on some platforms:
+MSVC 9.
address@hidden
On Windows platforms (excluding Cygwin), @code{socket} and @code{accept}
followed by @code{fdopen} do not return streams that can be closed by
@code{fclose}.
--- lib/fclose.c.orig Sun Sep 25 12:04:17 2011
+++ lib/fclose.c Sun Sep 25 12:00:47 2011
@@ -23,12 +23,37 @@
#include <unistd.h>
#include "freading.h"
+#include "msvc-inval.h"
+
+#undef fclose
+
+#if HAVE_MSVC_INVALID_PARAMETER_HANDLER
+static int
+fclose_nothrow (FILE *fp)
+{
+ int result;
+
+ TRY_MSVC_INVAL
+ {
+ result = fclose (fp);
+ }
+ CATCH_MSVC_INVAL
+ {
+ result = EOF;
+ errno = EBADF;
+ }
+ DONE_MSVC_INVAL;
+
+ return result;
+}
+#else
+# define fclose_nothrow fclose
+#endif
/* Override fclose() to call the overridden fflush() or close(). */
int
rpl_fclose (FILE *fp)
-#undef fclose
{
int saved_errno = 0;
int fd;
@@ -37,7 +62,7 @@
/* Don't change behavior on memstreams. */
fd = fileno (fp);
if (fd < 0)
- return fclose (fp);
+ return fclose_nothrow (fp);
/* We only need to flush the file if it is not reading or if it is
seekable. This only guarantees the file position of input files
@@ -55,7 +80,8 @@
if (close (fd) < 0 && saved_errno == 0)
saved_errno = errno;
- fclose (fp); /* will fail with errno = EBADF, if we did not lose a race */
+ fclose_nothrow (fp); /* will fail with errno = EBADF,
+ if we did not lose a race */
#else /* !WINDOWS_SOCKETS */
/* Call fclose() and invoke all hooks of the overridden close(). */
@@ -64,12 +90,12 @@
/* Note about multithread-safety: There is a race condition here as well.
Some other thread could open fd between our calls to fclose and
_gl_unregister_fd. */
- result = fclose (fp);
+ result = fclose_nothrow (fp);
if (result == 0)
_gl_unregister_fd (fd);
# else
/* No race condition here. */
- result = fclose (fp);
+ result = fclose_nothrow (fp);
# endif
#endif /* !WINDOWS_SOCKETS */
--- modules/fclose.orig Sun Sep 25 12:04:17 2011
+++ modules/fclose Sun Sep 25 12:01:21 2011
@@ -11,6 +11,7 @@
fflush [test $REPLACE_FCLOSE = 1]
freading [test $REPLACE_FCLOSE = 1]
lseek [test $REPLACE_FCLOSE = 1]
+msvc-inval [test $REPLACE_FCLOSE = 1]
configure.ac:
gl_FUNC_FCLOSE
--
In memoriam Safia Ahmed-jan <http://en.wikipedia.org/wiki/Safia_Ahmed-jan>
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- fclose on MSVC,
Bruno Haible <=