Fix SIGSEGV in std::print with a setvbuf'd FILE
std::print now correctly handles FILE streams set with setvbuf, preventing SIGSEGV.
This commit fixes a regression in std::print that caused a segmentation fault when formatting output to a FILE* stream that had been configured with setvbuf. The issue arose because the new P3107R5 implementation’s file sink logic did not properly handle the stream state previously managed by fwrite. The fix ensures that __overflow is called correctly during the initialization of the file sink’s write buffer when the write pointer is null, restoring proper stream handling.
In Details
This change corrects a regression in libstdc++'s std::print formatting to FILE* streams. The P3107R5 implementation's _File_sink constructor incorrectly assumed stream state, leading to SIGSEGV when _IO_write_ptr was null for setvbuf-affected streams. The fix involves ensuring __overflow is invoked in _M_init_write_buf before writing begins.
- std::print
- A C++23 standard library function for formatted output, similar to
printfbut with type safety and overload resolution. It can output to various targets, including streams andFILE*. - setvbuf
- A C standard library function that allows a program to control the buffering mode and buffer size for a given
FILEstream. Incorrect use or interaction with other stream operations can lead to undefined behavior. - SIGSEGV
- A signal generated when a program attempts to access a memory location that it is not allowed to access, typically indicating a segmentation fault and program crash.
- FILE*
- A pointer to a
FILEstructure, used in C and C++ to represent an input/output stream. It manages buffering and provides a consistent interface for file operations.