bug-gnulib
[Top][All Lists]
Advanced

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

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


From: Bastien ROUCARIES
Subject: Re: [PATCH] freopen SEGFAULT on win32 with filename == NULL
Date: Wed, 31 Aug 2011 12:10:04 +0200

On Wed, Aug 31, 2011 at 11:13 AM, Bastien ROUCARIES
<address@hidden> wrote:
> On Wed, Aug 31, 2011 at 10:49 AM, Bruno Haible <address@hidden> wrote:
>> Claudio Bley wrote:
>>> 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
>>>
>>> --------------------
>>
>> Unfortunately, freopen (NULL, ...) is not something gnulib can support
>> portably. POSIX:2008 specifies the effects of this call in
>> <http://pubs.opengroup.org/onlinepubs/9699919799/functions/freopen.html>:
>>
>>  If filename is a null pointer, the freopen() function shall attempt to
>>  change the mode of the stream to that specified by mode, as if the name
>>  of the file currently associated with the stream had been used. In this
>>  case, the file descriptor associated with the stream need not be closed
>>  if the call to freopen() succeeds. It is implementation-defined which
>>  changes of mode are permitted (if any), and under what circumstances.
>>
>> For testing, I added this line to the freopen test:
>>
>> *** tests/test-freopen.c.orig   Wed Aug 31 10:30:38 2011
>> --- tests/test-freopen.c        Wed Aug 31 10:25:43 2011
>> ***************
>> *** 29,34 ****
>> --- 29,35 ----
>>  main ()
>>  {
>>    ASSERT (freopen ("/dev/null", "r", stdin) != NULL);
>> +   ASSERT (freopen (NULL, "r", freopen ("/dev/null", "rw", stdin)) != NULL);
>>
>>    return 0;
>>  }
>>
>> The result is that it fails on several platforms
>> (OpenBSD 4.9, AIX 7.1, HP-UX 11.23, IRIX 6.5, OSF/1 5.1, Solaris 10, mingw):
>> test-freopen.c:32: assertion failed
>> FAIL: test-freopen
>>
>> And implementing this requires a conversion from file descriptor to filename
>> (at least, that's what the glibc implementation of freopen (NULL, ...) uses),
>> which is only available on very few operating systems.
>
> Time to abstract this functionnality ?
>
> I agree with portability goal but under windows we have the name using

More means to retrieve file name
- linux /proc/self/fd
- windows FileInformation  of GetFileInformationByHandleEx
- freebsd using FDESCFS /dev/fd or proc
- solaris using FDESCFS or proc
- hpux has pstat
- /proc/pid/fd/ under Aix (no self)
- osx as /dev/fd
- irix has /dev/fd

Bastien

>
> Bastien
>



reply via email to

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