Farid Zaripov wrote:
[...]
The basic_stringbuf<>::str (const char_type *, _RWSTD_SIZE_T)
have the assertions at begin and at end (sstream.cc line 72 and 167),
and
both assertions passes.
Ah, yes, thanks for the pointer! I was looking at the other
two overloads in the header that just call the one in the
.cc file and didn't realize that that one was the workhorse.
The bug is in that len calculated as highmark - pbase(), but pbase()
== 0
when buffer opened only in input mode. The begin of the buffer is always
this->_C_buffer with any openmode. So len = highmark - _C_buffer is
correct, I think.
In input mode (only) the function is supposed to return:
string(eback(), egptr());
In output mode (or input | output) the function must return:
string(pbase(), high_mark);
So unless I'm missing something the program below should be
a valid (albeit incomplete) test case. Let me know what you
think.
#include <cassert>
#include <cstdio>
#include <sstream>
int main ()
{
struct Buf: std::stringbuf {
Buf (std::string s, std::ios::openmode m)
: std::stringbuf (s, m) { }
void setget (int beg, int cur, int end) {
setg (eback () + beg, eback () + cur, eback () + end);
}
void setput (int beg, int cur, int end) {
setp (pbase () + beg, pbase () + end);
pbump (cur);
}
};
{
Buf buf ("abcde", std::ios::in);
buf.setget (1, 2, 4);
std::printf ("%s\n", buf.str ().c_str ());
assert ("bcd" == buf.str ());
}
{
Buf buf ("abcde", std::ios::out);
buf.setput (1, 2, 4);
std::printf ("%s\n", buf.str ().c_str ());
assert ("bcd" == buf.str ());
}
}
Martin