[PATCH] libstdc++/2913 & libstdc++/4879
Brad Garcia
garsh@attbi.com
Tue Dec 18 03:36:00 GMT 2001
The following patch fixes these two PR's.
When closing an ofstream, we currently don't detect any errors
that occur when flushing the remaining buffer, or when actually
closing the underlying file. Thus, we could close a file, and
the stream still has the "good" bit set, yet we failed to
actually write everything successfully to the file.
This change fixes both of these conditions. Please take a look
and comment. If the patch is accepted, please attribute it to
John Fardo.
--------------------------------
diff -u libstdc++/fstream.tcc new/fstream.tcc
--- libstdc++/fstream.tcc Mon Dec 17 15:57:02 2001
+++ new/fstream.tcc Mon Dec 17 15:55:46 2001
@@ -146,7 +146,13 @@
{
bool __testput = _M_out_cur && _M_out_beg < _M_out_end;
if (__testput)
- _M_really_overflow(traits_type::eof());
+ if (_M_really_overflow(traits_type::eof()) == traits_type::eof())
+ {
+ _M_file->close();
+ delete _M_file;
+ _M_file = NULL;
+ return __ret;
+ }
// NB: Do this here so that re-opened filebufs will be cool...
_M_pback_destroy();
@@ -177,6 +183,10 @@
// have it be opened.....
if (_M_file)
{
+ if (__ret && !_M_file->close())
+ {
+ __ret = NULL;
+ }
delete _M_file;
_M_file = NULL;
}
@@ -200,7 +210,7 @@
if (!__testeof)
__ret = _M_in_end - _M_in_cur;
}
- _M_last_overflowed = false;
+ _M_last_overflowed = false;
return __ret;
}
@@ -424,7 +434,11 @@
traits_type::to_char_type(__c);
// NB: Need this so that external byte sequence reflects
// internal buffer.
- _M_file->sync();
+ if (_M_file->sync() != 0)
+ {
+ return __ret;
+ }
+
if (__len == __plen)
{
_M_set_indeterminate();
diff -u libstdc++/std_fstream.h new/std_fstream.h
--- libstdc++/std_fstream.h Mon Dec 17 15:57:02 2001
+++ new/std_fstream.h Mon Dec 17 15:58:32 2001
@@ -177,7 +177,10 @@
{
// Make sure that libio resyncs its idea of the file position
// with the external file.
- _M_file->sync();
+ if (_M_file->sync() != 0)
+ {
+ return traits_type::eof();
+ }
// Need to restore current position. This interpreted as
// the position of the external byte sequence (_M_file)
@@ -185,7 +188,10 @@
// (_M_out_beg - _M_out_cur)
streamoff __cur = _M_file->seekoff(0, ios_base::cur);
off_type __off = _M_out_cur - _M_out_beg;
- _M_really_overflow();
+ if (_M_really_overflow() == traits_type::eof())
+ {
+ return traits_type::eof();
+ }
_M_file->seekpos(__cur + __off);
}
_M_last_overflowed = false;
More information about the Libstdc++
mailing list