https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81751

            Bug ID: 81751
           Summary: __basic_file::sync() may flush _all_ files
           Product: gcc
           Version: 7.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: bugzilla.volker at kabelmail dot de
  Target Milestone: ---

When creating a stdio_filebuf from a FILE* the constructor calls
__basic_file::sys_open() which in turn calls __basic_file::sync(). Sync()
unconditionally makes a call to fflush() even if the _M_cfile member is NULL
which then causes _all_ open streams to be sync'd.

This behaviour makes it impossible to use stdio_filebuf in a multithreaded
environment as there may be a buffer that is flushed multiple times.

My proposed solution to file libstdc++-v3/config/io/basic_file_stdio.cc is to
call fflush() in sync() only if _M_cfile is not NULL:

    int 
    __basic_file<char>::sync() 
    { return _M_cfile ? fflush(_M_cfile) : 0; }

This change makes sure only the controlled sequence is synchronized.

Alternatively the call to sync in function sys_open (in the same file) may be
made conditional on _M_cfile.

This issue is present in versions back to at least 4.7.2.

Reply via email to