This page is a snapshot from the LWG issues list, see the Library Active Issues List for more information and the meaning of CD1 status.

91. Description of operator>> and getline() for string<> might cause endless loop

Section: 27.4.4.4 [string.io] Status: CD1 Submitter: Nico Josuttis Opened: 1998-09-29 Last modified: 2016-01-28

Priority: Not Prioritized

View all other issues in [string.io].

View all issues with CD1 status.

Discussion:

Operator >> and getline() for strings read until eof() in the input stream is true. However, this might never happen, if the stream can't read anymore without reaching EOF. So shouldn't it be changed into that it reads until !good() ?

Proposed resolution:

In 27.4.4.4 [string.io], paragraph 1, replace:

Effects: Begins by constructing a sentry object k as if k were constructed by typename basic_istream<charT,traits>::sentry k( is). If bool( k) is true, it calls str.erase() and then extracts characters from is and appends them to str as if by calling str.append(1, c). If is.width() is greater than zero, the maximum number n of characters appended is is.width(); otherwise n is str.max_size(). Characters are extracted and appended until any of the following occurs:

with:

Effects: Behaves as a formatted input function (31.7.5.3.1 [istream.formatted.reqmts]). After constructing a sentry object, if the sentry converts to true, calls str.erase() and then extracts characters from is and appends them to str as if by calling str.append(1,c). If is.width() is greater than zero, the maximum number n of characters appended is is.width(); otherwise n is str.max_size(). Characters are extracted and appended until any of the following occurs:

In 27.4.4.4 [string.io], paragraph 6, replace

Effects: Begins by constructing a sentry object k as if by typename basic_istream<charT,traits>::sentry k( is, true). If bool( k) is true, it calls str.erase() and then extracts characters from is and appends them to str as if by calling str.append(1, c) until any of the following occurs:

with:

Effects: Behaves as an unformatted input function (31.7.5.4 [istream.unformatted]), except that it does not affect the value returned by subsequent calls to basic_istream<>::gcount(). After constructing a sentry object, if the sentry converts to true, calls str.erase() and then extracts characters from is and appends them to str as if by calling str.append(1,c) until any of the following occurs:

[Redmond: Made changes in proposed resolution. operator>> should be a formatted input function, not an unformatted input function. getline should not be required to set gcount, since there is no mechanism for gcount to be set except by one of basic_istream's member functions.]

[Curaçao: Nico agrees with proposed resolution.]

Rationale:

The real issue here is whether or not these string input functions get their characters from a streambuf, rather than by calling an istream's member functions, a streambuf signals failure either by returning eof or by throwing an exception; there are no other possibilities. The proposed resolution makes it clear that these two functions do get characters from a streambuf.