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

            Bug ID: 123100
           Summary: std::ostringstream:str() (r-value version) returns
                    garbage
           Product: gcc
           Version: 15.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: terra at gnome dot org
  Target Milestone: ---

https://godbolt.org/z/zPssxv67s

The code below should, as I understand it, print "Hello" three times, possibly
followed by 95 \0 characters each time.  However, the third times it prints
garbage for me.

It's the combination of the pubsetbuf call and the r-value call to str() that
triggers this for me.


#include <sstream>
#include <iostream>

int
main()
{
  std::ostringstream os;

  char buf[100] = {};
  os.rdbuf()->pubsetbuf(buf, sizeof buf);

  os << "Hello";

  std::cerr << os.str() << std::endl;
  std::cerr << os.view() << std::endl;
  std::cerr << std::move(os).str() << std::endl;
  return 0;
}

Reply via email to