libstdc++/4545: basic_ostream op<< should set badbit, not failbit

bkoz@gcc.gnu.org bkoz@gcc.gnu.org
Thu Oct 25 21:36:00 GMT 2001


Synopsis: basic_ostream op<< should set badbit, not failbit

Responsible-Changed-From-To: unassigned->bkoz
Responsible-Changed-By: bkoz
Responsible-Changed-When: Thu Oct 25 21:36:43 2001
Responsible-Changed-Why:
    Mine.
State-Changed-From-To: open->feedback
State-Changed-By: bkoz
State-Changed-When: Thu Oct 25 21:36:43 2001
State-Changed-Why:
    Brendan, thanks for  pointing this out.
    
    I think that badbit is set when the streambuf is null, and failbit is set when no characters were copied. I believe this is consistent with a careful read of the standard:
    
    1: If sb is null calls setstate(badbit)
    2: If the function inserts no characters, it calls setstate(failbit)
    
    
    Anyway. I believe the function should look like this:
    
      template<typename _CharT, typename _Traits>
        basic_ostream<_CharT, _Traits>& 
        basic_ostream<_CharT, _Traits>::operator<<(__streambuf_type* __sbin)
        {
          sentry __cerb(*this);
          if (__cerb)
    	{
    	  try
    	    {
    	      streamsize __xtrct = 0;
    	      if (__sbin)
    		{
    		  __streambuf_type* __sbout = this->rdbuf();
    		  __xtrct = __copy_streambufs(*this, __sbin, __sbout);
    		}
    	      else
    		this->setstate(ios_base::badbit);
    	      if (!__xtrct)
    		this->setstate(ios_base::failbit);
    	    }
    	  catch(exception& __fail)
    	    {
    	      // 27.6.2.5.1 Common requirements.
    	      // Turn this on without causing an ios::failure to be thrown.
    	      this->setstate(ios_base::badbit);
    	      if ((this->exceptions() & ios_base::badbit) != 0)
    		__throw_exception_again;
    	    }
    	}
          return *this;
        }

http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&pr=4545&database=gcc



More information about the Gcc-prs mailing list