This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
patch: fstreams should check results of close()
- To: libstdc++ at gcc dot gnu dot org
- Subject: patch: fstreams should check results of close()
- From: Phil Edwards <pedwards at disaster dot jaj dot com>
- Date: Wed, 10 Oct 2001 16:27:01 -0400
libstdc++/2913
http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view&pr=2913&database=gcc
deals with fstream not setting failbit when close() is explicitly called,
but the close fails. I have a patch which fixes this for the cstdio=stdio
case (no clue about cstdio=libio).
There's no way I can think of to write a testsuite entry for this; how
do we /force/ a call to, say, ofstream::close() to fail? I tested it
by playing mean games with file descriptors, but that wouldn't work in
the testsuite. The original test case of writing to a full filesystem
got somewhat reproduced on a full floppy, but the fact is that we don't
check for problems while closing a file, and we should.
The PR submitter feels that it's a regression from 2.95, and I agree.
Thoughts?
2001-10-10 Phil Edwards <pme@gcc.gnu.org>
* config/io/basic_file_stdio.h (__basic_file::~__basic_file): Don't
bother calling fflush(), it happens automatically later.
(__basic_file::is_open): Make it clear we're doing pointer compares.
(__basic_file::close): Check fclose() return value explicitly. Set
_M_cfile to NULL on failure so that is_open() returns false.
* include/bits/fstream.tcc (basic_filebuf::close): Call _M_file->
close() and check return value before deleting.
Index: config/io/basic_file_stdio.h
===================================================================
retrieving revision 1.1
diff -u -3 -p -r1.1 basic_file_stdio.h
--- config/io/basic_file_stdio.h 8 Aug 2001 02:48:58 -0000 1.1
+++ config/io/basic_file_stdio.h 10 Oct 2001 20:14:38 -0000
@@ -42,10 +42,7 @@ namespace std
__basic_file<_CharT>::~__basic_file()
{
if (this->is_open())
- {
- fflush(_M_cfile);
this->close();
- }
}
template<typename _CharT>
@@ -131,15 +128,16 @@ namespace std
template<typename _CharT>
bool
- __basic_file<_CharT>::is_open() { return _M_cfile != 0; }
+ __basic_file<_CharT>::is_open() { return _M_cfile != NULL; }
template<typename _CharT>
__basic_file<_CharT>*
__basic_file<_CharT>::close()
{
__basic_file* __retval = static_cast<__basic_file*>(NULL);
- if (_M_cfile_created && fclose(_M_cfile))
+ if (_M_cfile_created && (fclose(_M_cfile) == 0))
__retval = this;
+ _M_cfile = NULL;
return __retval;
}
Index: include/bits/fstream.tcc
===================================================================
retrieving revision 1.16
diff -u -3 -p -r1.16 fstream.tcc
--- include/bits/fstream.tcc 20 Jul 2001 00:09:31 -0000 1.16
+++ include/bits/fstream.tcc 10 Oct 2001 20:14:38 -0000
@@ -202,8 +202,10 @@ namespace std
// Can actually allocate this file as part of an open and never
// have it be opened.....
- if (_M_file)
+ if (_M_file && __ret)
{
+ if (!_M_file->close())
+ __ret = NULL;
delete _M_file;
_M_file = NULL;
}