Re: fclose(0)
CBFalconer wrote:
Yes. But is it an "error" to pass NULL to fclose? Elsewhere Richard
Tobin says that undefined behaviour is invoked when NULL is passed to a
library function whose definition does not *explicitly* accept NULL as
a valid parameter. The above description of fclose says nothing about a
call with NULL as argument, so presumably, undefined behaviour is
invoked at the point fclose(0) is called. One outcome would be to check
for NULL and return EOF (which DigitalMars seems to do), another would
be to blindly process the argument leading to God knows what.
CBFalconer wrote:
Ian Collins wrote:
... snip ...
>
Says the standard.
>
7.19.5.1 The fclose function
>
Synopsis
[#1]
#include <stdio.h>
int fclose(FILE *stream);
>
Description
>
[#2] The fclose function causes the stream pointed to by
stream to be flushed and the associated file to be closed.
Any unwritten buffered data for the stream are delivered to
the host environment to be written to the file; any unread
buffered data are discarded. The stream is disassociated
from the file. If the associated buffer was automatically
allocated, it is deallocated.
>
Returns
>
[#3] The fclose function returns zero if the stream was
successfully closed, or EOF if any errors were detected.
>
Note the phrase "any errors" in the line above.
>CBFalconer wrote:
>>
>>
>>
>>
>Says who? The behaviour is undefined.
>>However fclose doesn't crash on receiving a NULL. It returns EOF.
>Says who? The behaviour is undefined.
Says the standard.
>
7.19.5.1 The fclose function
>
Synopsis
[#1]
#include <stdio.h>
int fclose(FILE *stream);
>
Description
>
[#2] The fclose function causes the stream pointed to by
stream to be flushed and the associated file to be closed.
Any unwritten buffered data for the stream are delivered to
the host environment to be written to the file; any unread
buffered data are discarded. The stream is disassociated
from the file. If the associated buffer was automatically
allocated, it is deallocated.
>
Returns
>
[#3] The fclose function returns zero if the stream was
successfully closed, or EOF if any errors were detected.
>
Note the phrase "any errors" in the line above.
Tobin says that undefined behaviour is invoked when NULL is passed to a
library function whose definition does not *explicitly* accept NULL as
a valid parameter. The above description of fclose says nothing about a
call with NULL as argument, so presumably, undefined behaviour is
invoked at the point fclose(0) is called. One outcome would be to check
for NULL and return EOF (which DigitalMars seems to do), another would
be to blindly process the argument leading to God knows what.
Comment