libstdc++/8258: basic_istream::readsome() with default buffer change stream state to ios_base::eofbit

johnb@stl.sarov.ru johnb@stl.sarov.ru
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
>Closed-Date:
>Last-Modified:
>Originator:     Eugeny Belov
>Release:        g++ v3.2
>Organization:
>Environment:
Red Hat linux 8.0 release
>Description:
  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 (27.6.1.3.30) 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();

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

3.2:
if (__num > 0)
 { ...read 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.
>How-To-Repeat:
Compile attached testcaes with g++ v3.2 and run.
>Fix:

>Release-Note:
>Audit-Trail:
>Unformatted:
----gnatsweb-attachment----
Content-Type: application/octet-stream; name="istr_readsome.cpp"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="istr_readsome.cpp"

I2luY2x1ZGUgPGlzdHJlYW0+CiNpbmNsdWRlIDxjYXNzZXJ0PgoKdXNpbmcgbmFtZXNwYWNlIHN0
ZDsKCgpjbGFzcyBteWJ1ZiA6IHB1YmxpYyBiYXNpY19zdHJlYW1idWYgPGNoYXI+IHsKCX07Cgpp
bnQgbWFpbiAoKSAKIHsKICAgIGNoYXIgYXJyWzEwXTsKICAgIG15YnVmIHNidWY7CiAgICBiYXNp
Y19pc3RyZWFtIDxjaGFyLCBjaGFyX3RyYWl0czxjaGFyPiA+IGlzdHIoJnNidWYpOwoKICAgIGFz
c2VydCAoaXN0ci5yZHN0YXRlKCkgPT0gaW9zX2Jhc2U6Omdvb2RiaXQpOwogICAgYXNzZXJ0IChp
c3RyLnJlYWRzb21lKGFyciwgMTApID09IDApOwogICAgYXNzZXJ0IChpc3RyLnJkc3RhdGUoKSA9
PSBpb3NfYmFzZTo6Z29vZGJpdCk7CgogIHJldHVybiAwOwogfQoK



More information about the Gcc-bugs mailing list