🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: gnu.g++.lib.bug
Article #47

iostream bug and fix

#47
From: Jason Merrill
Date: Sat, 28 Mar 1992 20:36
65 lines
2117 bytes
The iostream classes in libg++ 2.0 have a bug in them when reading an EOF,
as illustrated by this program:

#include <iostream.h>
main()
{
  char buf[100];
  int i = 0;
  while( cin >> buf )
    cout << i++;
}

The program will happily read in input until EOF, then read another string,
then exit.  This is due to this chain of operation:

operator>>(istream,char*) calls istream::ipfx0()
istream::ipfx0() calls ws(istream&)
ws(istream&) calls streambuf::sbumpc()
sbumpc() calls underflow()
underflow() calls ::read()

at this point, underflow realizes that we're at EOF and sets a flag to that
effect.  So it returns EOF to ws.

ws sets ios::eofbit for cin, and returns.
ipfx0() returns happily to operator>>

operator>> calls sbumpc()
sbumpc() calls underflow()
underflow() calls ::read()
read() returns the string after the EOF
underflow() returns
sbumpc() returns the first character after the EOF
etc.

I'm not sure where the problem should be fixed.  Should ipfx0() return 1 if
the stream is at EOF?  Should sbumpc check ios::eofbit?  Or should
underflow() check its own flag?

I decided that underflow() should be responsible.  Does anyone disagree?

*** /staff/jason/oldfilebuf.C	Sat Mar 28 12:34:34 1992
--- /gnu/libg++-2.0/libg++/iostream/filebuf.C	Sat Mar 28 02:17:58 1992
***************
*** 269,274
  	return (EOF);
  #endif
  
      if ((xflags() & _S_CAN_READ) == 0) // SET ERROR
  	return EOF;
    retry:

--- 269,276 -----
  	return (EOF);
  #endif
  
+     if (xflags() & _S_EOF_SEEN) return EOF;
+ 
      if ((xflags() & _S_CAN_READ) == 0) // SET ERROR
  	return EOF;
    retry:

--
Jason Merrill
jason@jarthur.claremont.edu
Technical Details
Message-ID: <1992Mar28.203649.1984@muddcs.claremont.edu>
Path: arc1-ca.pugleaf.net!sparky!uunet!cis.ohio-state.edu!jarthur.claremont.edu!jason

Show Full Headers
Complete Article Headers
Newsgroups: gnu.g++.lib.bug
Path: arc1-ca.pugleaf.net!sparky!uunet!cis.ohio-state.edu!jarthur.claremont.edu!jason
From: jason@jarthur.claremont.edu (Jason Merrill)
Subject: iostream bug and fix
Message-ID: <1992Mar28.203649.1984@muddcs.claremont.edu>
Sender: gnulists@ai.mit.edu
Organization: Harvey Mudd College
Distribution: gnu
Date: Sat, 28 Mar 1992 20:36:49 GMT
Approved: bug-lib-g++@prep.ai.mit.edu
Lines: 66