libstdc++/8258: basic_istream::readsome() with default buffer change stream state to ios_base::eofbit
Thu Oct 17 09:56:00 GMT 2002

>Number:         8258
>Category:       libstdc++
>Synopsis:       basic_istream::readsome() with default buffer change stream state to ios_base::eofbit
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Thu Oct 17 09:56:00 PDT 2002
>Originator:     Eugeny Belov
>Release:        g++ v3.2
Red Hat linux 8.0 release
  Possibly this is not a bug, but the g++ v3.1 version of basic_istream::readsome() don`t change the stream state in case of using default basic_streambuf constructor (zero length buffer). Here is an example:

#include <istream>
#include <cassert>

using namespace std;

class mybuf : public basic_streambuf <char> {

int main () 
    char arr[10];
    mybuf sbuf;
    basic_istream <char, char_traits<char> > istr(&sbuf);

    assert (istr.rdstate() == ios_base::goodbit);
    assert (istr.readsome(arr, 10) == 0);
    assert (istr.rdstate() == ios_base::goodbit);

  return 0;

  The result here is that the last assertion is failing with g++ v3.2.
  I found in ISO/IEC 14882 C++ standard ( only 2 possibilities of changing the stream state related to readsome():
1. if !good() - setstate (failbit)
2. if rdbuf()->in_avail() == -1 - setstate (eofbit)
  The example above gives:
1. good() == true
2. rdbuf()->in_avail() == 0 (!= -1)
so, I can expect that stream state will not change.
  I made a diff for 3.1 and 3.2 versions of bits/istream.tcc and noted that here are the following changes which give such unexpected (for me) behavior:

streamsize __num = this->rdbuf()->in_avail();

if (__num != static_cast <streamsize>(__eof))
 { from buffer... }
else this->setstate (ios_base::eofbit)

if (__num > 0)
 { from buffer... }
else this->setstate (ios_base::eofbit)

  Shure, changing the stream state in such case can be useful, but my opinion is that previous version was more standard conformant.
Compile attached testcaes with g++ v3.2 and run.

Content-Type: application/octet-stream; name="istr_readsome.cpp"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="istr_readsome.cpp"


More information about the Gcc-bugs mailing list