Uploaded image for project: 'C++ Standard Library'
  1. C++ Standard Library
  2. STDCXX-250

std::operator>>(istream&, string&) fails to set failbit after it extracts 0 characters

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Minor
    • Resolution: Fixed
    • Affects Version/s: 4.1.2, 4.1.3
    • Fix Version/s: 4.2.1
    • Component/s: 21. Strings
    • Labels:
      None
    • Environment:

      all

    • Patch Info:
      Patch Available
    • Severity:
      Incorrect Behavior

      Description

      21.3.7.9, p3 says about the string extractor: "If the function extracts no characters, it calls is.setstate(ios::failbit), which may throw ios_base::failure (27.4.4.3)." The test program below shows that in unbuffered mode stdcxx fails to do so when an exception is thrown during the third call to underflow().

      $ cat v.cpp && make v && ./v
      #include <cassert>
      #include <cstdio>
      #include <istream>
      #include <string>

      int main ()
      {
      struct: std::streambuf {
      int_type underflow ()

      { static int i = 0; // i == 0: sgect() invoked from sentry ctor // i == 1: sgetc() invoked from operator>>() // i == 2: sbumpc() invoked from operator>>() return 1 < i++ ? throw i : 'x'; }

      } buf;

      std::istream is (&buf);

      std::string s;
      is >> s;

      std::printf ("state = %c%c%c, string = \"%s\" (length %u)\n",
      is.rdstate () & is.badbit ? 'B' : '-',
      is.rdstate () & is.eofbit ? 'E' : '-',
      is.rdstate () & is.failbit ? 'F' : '-',
      s.c_str (), s.size ());

      assert ("x" == s);
      assert ((is.failbit | is.badbit) == is.rdstate ());
      }
      gcc -c -I/build/sebor/dev/stdlib/include/ansi -D_RWSTDDEBUG -pthreads -D_RWSTD_USE_CONFIG -I/build/sebor/dev/stdlib/include -I/build/sebor/gcc-4.1.0-15s/include -I/build/sebor/dev/stdlib/../rwtest -I/build/sebor/dev/stdlib/../rwtest/include -I/build/sebor/dev/stdlib/tests/include -pedantic -nostdinc++ -g -W -Wall -Wcast-qual -Winline -Wshadow -Wwrite-strings -Wno-long-long v.cpp
      gcc v.o -o v -L/build/sebor/gcc-4.1.0-15s/rwtest -lrwtest15s -pthreads -L/build/sebor/gcc-4.1.0-15s/lib -lstd15s -lsupc++ -lm
      state = B--, string = "x" (length 1)
      Assertion failed: (is.failbit | is.badbit) == is.rdstate (), file v.cpp, line 30
      Abort (core dumped)

        Attachments

        1. istream.cc.diff
          1 kB
          Farid Zaripov

          Activity

            People

            • Assignee:
              farid Farid Zaripov
              Reporter:
              sebor Martin Sebor
            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: