https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67600
Bug ID: 67600 Summary: [5/6] Segfault when assigning only one char to ostreambuf_iterator<char> compiled with -O2 or -O3 Product: gcc Version: 6.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: libstdc++ Assignee: unassigned at gcc dot gnu.org Reporter: cctsai57 at gmail dot com Target Milestone: --- #include <iostream> #include <sstream> int main() { std::basic_ostringstream<char> ostr; std::ostreambuf_iterator<char> iter(ostr.rdbuf()); *iter++ = 'X'; // Segmentation fault if -O2 or -O3. // *iter++ = 'Y'; // But pass if adding this statement. std::cout << ostr.str() << '\n'; return 0; } Description: If the above simplified code is compiled with -O2 or -O3, segmentation fault happens in "*iter++ = 'x'" statement, but -O0, -O1 or -Os are O.K. If I add the "*iter++ = 'Y'" statement and compile it with -O2 or -O3, it can print "XY" successfully. gcc versions: Fails on gcc-5 (x86_64-redhat-linux on Fedora-22) and gcc-6.0.0 (20150916 (experimental)). Pass on gcc-4.8 and gcc-4.9