<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/59083>59083</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Invalid output when using wbuffer_convert with ostringstream
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            libc++
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          ldionne
      </td>
    </tr>
</table>

<pre>
    The following test case was extracted from `libcxx/test/std/localization/locales/locale.convenience/conversions/conversions.buffer/overflow.pass.cpp`: https://godbolt.org/z/Wc73box63

```c++
#include <locale>
#include <codecvt>
#include <fstream>
#include <cassert>

#include <iostream>
#include <sstream>


struct test_buf
    : public std::wbuffer_convert<std::codecvt_utf8<wchar_t> >
{
    typedef std::wbuffer_convert<std::codecvt_utf8<wchar_t> > base;
    typedef base::char_type   char_type;
    typedef base::int_type    int_type;
    typedef base::traits_type traits_type;

    explicit test_buf(std::streambuf* sb) : base(sb) {}

    char_type* pbase() const {return base::pbase();}
    char_type* pptr()  const {return base::pptr();}
    char_type* epptr() const {return base::epptr();}
    void gbump(int n) {base::gbump(n);}

    virtual int_type overflow(int_type c = traits_type::eof()) {return base::overflow(c);}
};

int main() {
    std::ostringstream out;
    test_buf f(out.rdbuf());
    assert(f.sputc(0x4E51) == 0x4E51);
    assert(f.sputc(0x4E52) == 0x4E52);
    assert(f.sputc(0x4E53) == 0x4E53);

    std::string s = out.str();
    std::cout << "s = '" << s << "', s.size() = " << s.size() << std::endl;
}
```

MSVC output: `s = '乑乒乓', s.size() = 9`
GCC output: `s = '', s.size() = 0`
Libc++ output: `s = '乑', s.size() = 3`

Only the MSVC output makes sense IMO.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJylVltvozgU_jXwYg0Ck4TwwEPadEYj7WgedrX7WIE5JN51bIRNkvbX7zHmFtq0I01FiTnnfJ_PzZdClS_ZX0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