This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug libstdc++/81751] __basic_file::sync() may flush _all_ files
- From: "redi at gcc dot gnu.org" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Wed, 09 Aug 2017 12:04:13 +0000
- Subject: [Bug libstdc++/81751] __basic_file::sync() may flush _all_ files
- Auto-submitted: auto-generated
- References: <bug-81751-4@http.gcc.gnu.org/bugzilla/>
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81751
--- Comment #4 from Jonathan Wakely <redi at gcc dot gnu.org> ---
But there is an open FILE and it might have pending writes or ungetc'd data
that should be flushed. I think that's why it's there. Consider:
FILE* f = std::fopen("81751.txt", "w+");
std::fwrite("Some words.", 1, 10, f);
__gnu_cxx::stdio_filebuf<char> buf(f, std::ios::in|std::ios::out, BUFSIZ);
int c = buf.sgetc();
If the constructor doesn't flush then the read performed by sgetc will follow
the write without an intervening seek or flush, which is undefined behaviour.
The constructor taking a file descriptor creates a new FILE using fdopen, so
there are no buffered writes on the stream, and a flush isn't needed.