bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] freopen SEGFAULT on win32 with filename == NULL


From: Claudio Bley
Subject: [PATCH] freopen SEGFAULT on win32 with filename == NULL
Date: Thu, 25 Aug 2011 21:52:59 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux)

Hi.

When calling 

freopen(NULL, mode, stream);

on MS Windows using MinGW segfaults because it tries to strcmp(NULL,
"/dev/null")... *ouch*

--------------------
--- freopen.c.orig      2011-08-03 14:22:15 +0200
+++ freopen.c   2011-08-25 21:01:46 +0200
@@ -38,7 +38,7 @@
 rpl_freopen (const char *filename, const char *mode, FILE *stream)
 {
 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
-  if (strcmp (filename, "/dev/null") == 0)
+  if (filename && strcmp (filename, "/dev/null") == 0)
     filename = "NUL";
 #endif
 
--------------------

Furthermore, using NULL as filename does not work at all using the MS
C runtime library (debug assertion violation).

--------------------
--- freopen.c.1 2011-08-25 21:05:34 +0200
+++ freopen.c   2011-08-25 21:08:52 +0200
@@ -38,6 +38,9 @@
 rpl_freopen (const char *filename, const char *mode, FILE *stream)
 {
 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
+#  ifdef _MSC_VER
+  if (!filename) return NULL; /* would trigger a runtime error on MSVC */
+#  endif
   if (filename && strcmp (filename, "/dev/null") == 0)
     filename = "NUL";
 #endif

--------------------

Using MinGW and trying to change stdout's mode to binary simply fails:

if (!isatty(fileno(stdout))
   freopen(NULL, "wb", stdout); /* <- returns NULL */

Would it be feasible to use the _setmode function in rpl_freopen
instead of freopen if the filename == NULL on win32 (non-cygwin)?

What about the fsetbinary, xfchangemode functions discussed here
[http://lists.gnu.org/archive/html/bug-gnulib/2010-03/msg00111.html]?


Cheers,
-- 
Claudio




reply via email to

[Prev in Thread] Current Thread [Next in Thread]