As mentioned in bug 38476, [lib.istream], p2 specifies that:

  Both [formatted and unformatted] input functions are described as if
  they obtain (or extract) input characters by calling rdbuf()->sbumpc()
  or rdbuf()->sgetc(). They may use other public members of istream.

sgetc() is required to return the result of underflow() and sbumpc() is
required to return the result of uflow(). An implementation of
istream::read() may call streambuf::xsgetn() but it must do so in a way
that achieves the "as if" effect described above. Notably, it must avoid
calling an xsgetn() overridden in a derived class. The test case below
shows that the gcc implementation fails to do so.

$ cat z.cpp && g++ z.cpp && ./a.out 
#include <cassert>
#include <istream>
#include <streambuf>

int main ()
{
    static int x = '0';

    struct: std::streambuf {
        char c;

        int_type underflow () {
            c = x++;
            setg (&c, &c, &c + 1);
            return c;
        }

        std::streamsize xsgetn (char*, std::streamsize) {
            assert (!"xsgetn should not be called");
            return 0;
        }
    } sb;

    std::istream in (&sb);

    char s [4] = "";

    in.read (s, sizeof s);

    assert (in.good ());
    assert (sizeof s == in.gcount ());
    assert ('0' == s [0] && '1' == s [1] && '2' == s [2] && '3' == s [3]);
}
a.out: z.cpp:19: virtual std::streamsize main()::<anonymous
struct>::xsgetn(char*, std::streamsize): Assertion `!"xsgetn should not be
called"' failed.
Aborted


-- 
           Summary: istream::read() calls streambuf::xsgetn()
           Product: gcc
           Version: 4.3.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: sebor at roguewave dot com


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38678

Reply via email to