https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89452
Bug ID: 89452
Summary: basic_stringbuf::seekoff and basic_stringbuf::seekpos
implementations
Product: gcc
Version: 7.4.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: libstdc++
Assignee: unassigned at gcc dot gnu.org
Reporter: nknikita at niisi dot ras.ru
Target Milestone: ---
It seems that basic_stringbuf::seekoff and basic_stringbuf::seekpos
implementations are inconsistent with the requirements of the standard.
As far as I can see, the standard ensures the execution of the following code
will not be terminated:
std::stringbuf sb("abcdefgh", std::ios_base::in);
assert(sb.pubseekpos(3, std::ios_base::in | std::ios_base::out) == -1);
According to ISO/IEC 14882:2017(E):
1. basic_streambuf::pubseekpos calls seekpos member function of the most
derived class, i.e., basic_stringbuf::seekpos.
2. basic_stringbuf::seekpos (3, std::ios_base::in | std::ios_base::out)
effectively calls basic_stringbuf::seekoff (3, std::ios_bas::beg,
std::ios_base::in | std::ios_base::out).
3. basic_stringbuf::seekoff identifies what sequences should be positioned.
Since std::ios_base::in | std::ios_base::out is passed as which and
ios_base::beg is passed as way, both the input and the output sequences should
be affected ([stringbuf.virtuals]/9).
4. pptr() is a null pointer. Paragraphs [stringbuf.cons]/3, [streambuf.cons]/1,
[stringbuf. members]/3 guarantee it.
5. [stringbuf.virtuals]/10 specifies that the positioning operation fails,
i.e., pos_type(-1) should be returned.
However, seekoff/seekpos implementations impose some additional requirements
while identifying sequences to be positioned:
bool __testin = (ios_base::in & this->_M_mode & __mode) != 0;
bool __testout = (ios_base::out & this->_M_mode & __mode) != 0;
that is, the openmode is also checked. The input/output sequence therefore will
not be positioned if stringbuf openmode does not include
ios_base::in/ios_base::out. In the example given, the input sequence will be
positioned without an error (an unexpected behavior).