This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug libstdc++/81751] __basic_file::sync() may flush _all_ files


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.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]