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 4484
On 26 Apr 2008 at 15:06, Bartc wrote:
int status;
status=fclose(0 );
fclose takes a pointer argument, so this is the same as
status=fclose(N ULL);
which is obviously not a good idea.
[guess] Instead of fclose, did you mean close, which takes a file
descriptor as an argument? Are you wondering about whether it's OK to
close(0) (i.e. close the stdin stream)? If so, the answer is yes: it's
useful for piping data between parent and child processes. (You can use
dup() to bind the output of a pipe back to stdin.)
"Antoninus Twink" <no****@nospam. invalidwrote in message
news:sl******** ***********@nos pam.invalid...
On 26 Apr 2008 at 15:06, Bartc wrote:
>int status;
status=fclose( 0);
fclose takes a pointer argument, so this is the same as
status=fclose(N ULL);
which is obviously not a good idea.
[guess] Instead of fclose, did you mean close, which takes a file
descriptor as an argument? Are you wondering about whether it's OK to
close(0) (i.e. close the stdin stream)? If so, the answer is yes: it's
useful for piping data between parent and child processes. (You can use
dup() to bind the output of a pipe back to stdin.)
No, it came up in something like:
f=fopen(...);
fclose(f);
where the fopen failed. I would just have expected fclose to do nothing
given a null file handle (as I tend to do in my own functions that use
handles).
No big deal, I already use wrappers and it's a one-line fix. Was just mildly
surprised that that one line isn't already there.
--
Bartc
"Bartc" <bc@freeuk.comw rites:
status=fclose(0 );
This yields undefined behavior. Are you thinking of
fflush(NULL), which is well-defined?
--
"Given that computing power increases exponentially with time,
algorithms with exponential or better O-notations
are actually linear with a large constant."
--Mike Lee
Bartc said:
>
"Richard Heathfield" <rj*@see.sig.in validwrote in message
news:dZ******** *************** *******@bt.com. ..
>Bartc said:
>>This short program:
#include <stdio.h> #include <stdlib.h>
int main(void) { int status;
status=fclose (0);
printf("fclos e(0) status: %d\n",status);
}
crashes on the first two implementations I tried.
The Standard requires that you pass to fclose a pointer to a stream object of type FILE. You did not do this. (0 doesn't point to any object at all, let alone a stream object.) You broke the rules, so all bets are off.
fopen() returns a FILE* value, which can include NULL. So it's argueable
that NULL (or (FILE*)NULL) is acceptable, type-wise, to fclose().
No, it isn't, because the Standard doesn't say so. The Standard says that,
if you call fclose, you must pass to it a pointer to a stream object of
type FILE that has a file associated with it. You didn't do this.
<snip>
--
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
"Bartc" <bc@freeuk.comw rites:
[...]
>On 26 Apr 2008 at 15:06, Bartc wrote:
>>int status;
status=fclose (0);
[...]
>
No, it came up in something like:
f=fopen(...);
fclose(f);
where the fopen failed. I would just have expected fclose to do nothing
given a null file handle (as I tend to do in my own functions that use
handles).
In that case, it's probably just as easy to avoid calling fclose if
the fopen failed than to call it. You have to test whether fopen
succeeded anyway; if it doesn't, you don't attempt to read any input
from the file, right? So you have to do something like this:
f = fopen(...);
if (f != NULL) {
fread(...);
fclose(...);
}
else {
/* Report that you couldn't open the file */
}
No big deal, I already use wrappers and it's a one-line fix. Was just mildly
surprised that that one line isn't already there.
Yeah, most standard libary functions don't check for invalid input.
free(NULL) is a rare exception.
Note that, in the case of fclose(), checking for a null pointer
argument would just handle one failure case. There are plenty of
other errors that can't reasonably be caught, such as calling fclose()
with an uninitialized pointer.
--
Keith Thompson (The_Other_Keit h) <ks***@mib.or g>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Bartc wrote:
"Richard Heathfield" <rj*@see.sig.in validwrote
>> The Standard requires that you pass to fclose a pointer to a stream object of type FILE. You did not do this. (0 doesn't point to any object at all, let alone a stream object.) You broke the rules, so all bets are off.
fopen() returns a FILE* value, which can include NULL. So it's argueable
that NULL (or (FILE*)NULL) is acceptable, type-wise, to fclose().
The type is acceptable, but the value is not. Would
you expect `fprintf (NULL, "Hello, world!\n");' to work?
How about `fprintf (NULL, NULL);'?
The relevant passage is Section 7.1.4 paragraph 1:
"Each of the following statements applies unless
explicitly stated otherwise [...]: If an argument to
a function has an invalid value (such as [...] a null
pointer, [...]) [...]the behavior is undefined. [...]"
--
Eric Sosman es*****@ieee-dot-org.invalid
Bartc wrote:
[...]
What's wrong with fclose() doing something like: "ABORTING: Invalid handle
to fclose()".
It does! The spelling of the error message differs somewhat
implementation to implementation; on one I'm familiar with, the
message is spelled "SIGSEGV" ...
--
Eric Sosman es*****@ieee-dot-org.invalid
In comp.lang.c, Eric Sosman wrote:
Bartc wrote:
>[...] What's wrong with fclose() doing something like: "ABORTING: Invalid handle to fclose()".
It does! The spelling of the error message differs somewhat
implementation to implementation; on one I'm familiar with, the
message is spelled "SIGSEGV" ...
and I've seen that message, but spelled as "S0C4"
--
Lew Pitcher
Master Codewright & JOAT-in-training | Registered Linux User #112576 http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------
"Eric Sosman" <es*****@ieee-dot-org.invalidwrot e in message
news:XI******** *************** *******@comcast .com...
Bartc wrote:
>"Richard Heathfield" <rj*@see.sig.in validwrote
>>> The Standard requires that you pass to fclose a pointer to a stream object of type FILE. You did not do this. (0 doesn't point to any object at all, let alone a stream object.) You broke the rules, so all bets are off.
The relevant passage is Section 7.1.4 paragraph 1:
"Each of the following statements applies unless
explicitly stated otherwise [...]: If an argument to
a function has an invalid value (such as [...] a null
pointer, [...]) [...]the behavior is undefined. [...]"
I suspect this was written when there were already many implementations that
behaved this way.
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.
--
Bartc
Bartc said:
<snip>
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.
Crashing is not an idea - either good or bad. It is merely a consequence.
If a programmer is dense enough to break the contract between himself and
the implementation, he has no business complaining when something happens
that he doesn't like. If you don't want a crash or some other bad
consequence of passing an invalid argument (and neither do I), the answer
is simple: ***don't pass an invalid argument***.
--
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: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |