This short program:
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int status;
status=fclose(0 );
printf("fclose( 0) status: %d\n",status);
}
crashes on the first two implementations I tried. Then I tried DMC and that
gave the expected EOF status.
Given the emphasis on error-checking in this group, it seems astonishing
that a library function (executed at most once per file) cannot do this
elementary check on it's parameter.
(It's possible a zero FILE* handle is returned by fopen() but the file has
to be closed for other reasons before it can checked by the application.)
Was I unlucky or is it normal for C library functions to be so fragile?
-- Bart
Jun 27 '08
53 4488
Bartc wrote:
>
This short program:
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int status;
status=fclose(0 );
printf("fclose( 0) status: %d\n",status);
}
crashes on the first two implementations I tried. Then I tried
DMC and that gave the expected EOF status.
Of course. fclose requires a non-NULL FILE* parameter.
Given the emphasis on error-checking in this group, it seems
astonishing that a library function (executed at most once per
file) cannot do this elementary check on it's parameter.
(It's possible a zero FILE* handle is returned by fopen() but
the file has to be closed for other reasons before it can
checked by the application.)
No, because if fopen returns a NULL (not a zero) it means the file
has not opened.
--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home .att.net>
Try the download section.
** Posted from http://www.teranews.com **
Ben Bacarisse wrote:
>
.... snip ...
>
It's normal. Have you tried strlen(0), strcpy(0, 0) or fopen(0, 0)?
Luck is another matter. In a way you were lucky that at least one
implementation flagged up the call (with a crash). If you'd used
one that just returns EOF (which is allowed), you'd have no warning
that the construct is not portable.
Sure you would. The EOF is not a normal return value. Something
failed.
--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home .att.net>
Try the download section.
** Posted from http://www.teranews.com **
Bartc <bc@freeuk.comw rote:
>
If this was the case then it would be better to admit it rather than
suggest, as a few people have, that crashing on passing a null pointer is
actually a good idea.
It *is* a good idea: it forces programmers to fix their defective code.
-Larry Jones
I don't NEED to compromise my principles, because they don't have
the slightest bearing on what happens to me anyway. -- Calvin
CBFalconer wrote:
Chris Torek wrote:
>Eric Sosman <es*****@ieee-dot-org.invalidwrot e:
.... snip ...
>A burden and a benefit, simultaneously. :-)
>>The good idea is not to pass a null pointer in the first place.
And yet, if this is the case, why does free() accept NULL? The same argument holds here, and -- historically speaking -- at the time the C89 standard was being created, actual implementations actually crashed if you called free(NULL). So the committee have attempted to have it both ways: "We trust you (putting the burden of checking for NULL first) when you call fclose(), but we do not trust you (removing the burden of checking for NULL first) when you call free()".
However fclose doesn't crash on receiving a NULL. It returns EOF.
Says who? The behaviour is undefined.
--
Ian Collins. la************@ siemens.com said:
Bartc <bc@freeuk.comw rote:
>> If this was the case then it would be better to admit it rather than suggest, as a few people have, that crashing on passing a null pointer is actually a good idea.
It *is* a good idea:
No, it's merely a useful consequence.
it forces programmers to fix their defective code.
If that were true, it would be a pity that it isn't guaranteed by the
Standard. But it isn't true, of course. Some programmers never even see
the crashes, let alone fix their causes. For example, I have a web browser
written by someone else, which crashes whenever I accidentally click on a
PDF link and then click "cancel" on the subsequent dialog asking if I want
to hand over my firstborn to Adobe. Clearly the programmer never bothered
to test this execution path, so he has /not/ been forced to fix his
defective code.
--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
CBFalconer said:
<snip>
However fclose doesn't crash on receiving a NULL. It returns EOF.
$ cat foo.c
#include <stdio.h>
int main(void)
{
fclose(NULL);
printf("Hello, world!\n");
return 0;
}
$ ./foo
Segmentation fault (core dumped)
Undefined behaviour need not have the same effect on my system as it does
on yours. I was under the impression that you already knew that.
--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Richard Heathfield wrote:
la************@ siemens.com said:
>Bartc <bc@freeuk.comw rote:
>>If this was the case then it would be better to admit it rather than suggest, as a few people have, that crashing on passing a null pointer is actually a good idea.
It *is* a good idea:
No, it's merely a useful consequence.
>it forces programmers to fix their defective code.
If that were true, it would be a pity that it isn't guaranteed by the
Standard. But it isn't true, of course. Some programmers never even see
the crashes, let alone fix their causes. For example, I have a web browser
written by someone else, which crashes whenever I accidentally click on a
PDF link and then click "cancel" on the subsequent dialog asking if I want
to hand over my firstborn to Adobe. Clearly the programmer never bothered
to test this execution path, so he has /not/ been forced to fix his
defective code.
But he or she would if a paying customer filed a bug report. That is,
unless he or she works for a well known supplier of non-compliant browsers.
--
Ian Collins.
Ian Collins said:
Richard Heathfield wrote:
>la************@ siemens.com said:
>>Bartc <bc@freeuk.comw rote: If this was the case then it would be better to admit it rather than suggest, as a few people have, that crashing on passing a null pointer is actually a good idea. It *is* a good idea:
No, it's merely a useful consequence.
>>it forces programmers to fix their defective code.
If that were true, it would be a pity that it isn't guaranteed by the Standard. But it isn't true, of course. Some programmers never even see the crashes, let alone fix their causes. [example snipped] Clearly the programmer never bothered to test this execution path, so he has /not/ been forced to fix his defective code.
But he or she would if a paying customer filed a bug report.
So what you're saying is that the crash in itself is not sufficient to
force the programmer to fix the defective code. This doesn't appear to
contradict anything I said above.
--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
CBFalconer said:
Ian Collins wrote:
>CBFalconer wrote:
... snip ...
>>
>>However fclose doesn't crash on receiving a NULL. It returns EOF.
Says who? The behaviour is undefined.
Says the standard.
<snip>
>
[#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.
Note that the implementation is not *obliged* to detect the error of
passing NULL to this function. The Standard merely documents the result if
such an error /is/ detected. The behaviour if you pass NULL to fclose
remains undefined.
--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
CBFalconer said:
Richard Heathfield wrote:
>>
... snip ...
>> Undefined behaviour need not have the same effect on my system as it does on yours. I was under the impression that you already knew that.
See my reply to Ian.
See my reply to your reply to Ian.
--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: VB |
last post by:
Hi,
here File.cpp and File.h:
File.cpp:
----------------------
#pragma warning (disable: 4786)
|
by: Jeff |
last post by:
If this in not an ANSI-C question, then just flame me and I'll be on my
way....
I am using fdopen to associate a FILE with a socket descriptor. (Because
I've mentioned a socket descriptor I anticipate the flame. But I believe
this is a C question.) My problem is this: if the socket is closed and I
pass the FILE returned from fdopen to fclose, my program dies with a
memory fault. My question: why do I get a memory fault with fclose when...
|
by: collinm |
last post by:
hi
is it better to do:
FILE *fp;
if((fp = fopen(local_led, "r"))!=NULL)
{
fclose(fp);
}
else
|
by: lihua |
last post by:
Hi, Group!
I got one question here:
We all know that fclose() must be called after file operations to
avoid unexpected errors.But there are really cases when you forget to
do that!Just
like what happens in memory operations, everyone knows the importance
of
freeing the allocated memory, but there do have memory leaks from time
to
|
by: kathy |
last post by:
if fopen failed, does it necessary to call fclose?
I see an example like this:
....
stream = fopen(...);
if(stream == NULL)
{
....
}
else
| |
by: David Mathog |
last post by:
A program of mine writes to a tape unit. Output can be either through
stdout or through a file opened with fopen(). When all the data is
transferred to tape the program needs to close the output stream so that
the tape driver will write a filemark on the tape. Otherwise multiple
clumps of data saved to tape would all appear to be one big file on the
tape.
When the tape unit device was explicitly opened with fopen()
that's possible:...
|
by: Bill Cunningham |
last post by:
Is it really necessary to check the return type (int) or fclose ? fopen
I can understand but what about using
fflsuh(fp); /* fopen's pointer */
fclose(fp);
Would that take care of any unforseen truncation ?
Bill
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
| |
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |