Funniness with operator>> (istream&, string&)

scott snyder snyder@d0sgibnl1.fnal.gov
Sat Apr 1 00:00:00 GMT 2000


> OK. I was wrong. The looping part is a known issue. The setting of
> failbit for operator>> is not.
> 
> See:
> 
> 91.  Description of operator>> and getline() for string<> might cause
> endless loop
> 
> Sorry if I confused you with my tirade about istream::sentry and failbit.

Yes, i saw that -- but that looks like a different potential infinite
loop than the one i ran into.  That DR appears to be talking about
an infinite loop _inside_ operator>>, while mine is outside
the operator and, as you note, has to do with failing to set failbit.

> I really really really wish the standard was consistent WRT failbit.

I hear you...

Anyway, so it sounds like this is something which should get
submitted as a defect report.  I'll try to do that; it might be a few
days before i get to it, though.

In the meantime, i've patched my local version so that operator>>
does in fact set failbit if it fails to extract any characters, as
shown below.  That fixes the problem i was having, anyway.

thanks,
sss


2000-01-23  scott snyder  <snyder@fnal.gov>

	* bits/istream.tcc (operator>>(istream&, string&)): Set failbit if 
	we don't extract any characters.



Index: bits/istream.tcc
===================================================================
RCS file: /cvs/libstdc++/libstdc++/bits/istream.tcc,v
retrieving revision 1.36
diff -u -p -r1.36 istream.tcc
--- istream.tcc	2000/01/12 10:12:38	1.36
+++ istream.tcc	2000/01/26 06:51:38
@@ -1125,8 +1125,13 @@ namespace std {
 	  else
 	    __in.setstate(ios_base::eofbit);
 
+          if (!__extracted)
+            __in.setstate (ios_base::failbit);
+
 	  __in.width(0);
 	}
+      else
+        __in.setstate (ios_base::failbit);
       return __in;
     }
 


More information about the Libstdc++ mailing list