gcc/libstdc++-v3/src/c++98
Jonathan Wakely ba8fe4b483 libstdc++: Fix istream::ignore exit conditions (PR 94749, PR 96161)
My previous fix for PR 94749 did fix the reported case, so that the next
character is not discarded if it happens to equal the delimiter when __n
characters have already been read. But it introduced a new bug, which is
that the delimiter character would *not* be discarded if the number of
characters discarded is numeric_limits<streamsize>::max() or more before
reaching the delimiter.

The new bug happens because I changed the code to check _M_gcount < __n.
But when __n == numeric_limits<streamsize>::max() that is false, and so
we don't discard the delimiter. It's not sufficient to check for the
delimiter when the __large_ignore condition is true, because there's an
edge case where the delimiter is reached when _M_gcount == __n and so
we break out of the loop without setting __large_ignore.

PR 96161 is a similar bug to the original PR 94749 report, where eofbit
is set after discarding __n characters if there happen to be no more
characters in the stream.

This patch fixes both cases (and the regression) by checking different
conditions for the __n == max case and the __n < max case. For the
former case, we know that we must have either reached the delimiter or
EOF, and the value of _M_gcount doesn't matter (except to avoid integer
overflow). For the latter case we need to check _M_gcount first and only
set eofbit or discard the delimiter if it didn't reach __n. For the
latter case overflow can't happen because _M_gcount <= __n < max.

libstdc++-v3/ChangeLog:

	PR libstdc++/94749
	PR libstdc++/96161
	* include/bits/istream.tcc (basic_istream::ignore(streamsize))
	[n == max]: Check overflow conditions on _M_gcount. Rely on
	the fact that either EOF or the delimiter was reached.
	[n < max]: Check _M_gcount < n before checking for EOF or
	delimiter.
	(basic_istream::ignore(streamsize, char_type): Likewise.
	* src/c++98/compatibility.cc (istream::ignore(streamsize))
	(wistream::ignore(streamsize)): Likewise.
	* src/c++98/istream.cc (istream::ignore(streamsize, char_type))
	(wistream::ignore(streamsize, char_type)): Likewise.
	* testsuite/27_io/basic_istream/ignore/char/94749.cc: Check that
	delimiter is discarded if the number of characters ignored
	doesn't fit in streamsize.
	* testsuite/27_io/basic_istream/ignore/wchar_t/94749.cc:
	Likewise.
	* testsuite/27_io/basic_istream/ignore/char/96161.cc: New test.
	* testsuite/27_io/basic_istream/ignore/wchar_t/96161.cc: New test.
2020-07-13 12:09:44 +01:00
..
allocator-inst.cc Update copyright years. 2020-01-01 12:51:42 +01:00
bitmap_allocator.cc Update copyright years. 2020-01-01 12:51:42 +01:00
codecvt.cc Update copyright years. 2020-01-01 12:51:42 +01:00
compatibility-debug_list-2.cc Update copyright years. 2020-01-01 12:51:42 +01:00
compatibility-debug_list.cc Update copyright years. 2020-01-01 12:51:42 +01:00
compatibility-ldbl.cc Update copyright years. 2020-01-01 12:51:42 +01:00
compatibility.cc libstdc++: Fix istream::ignore exit conditions (PR 94749, PR 96161) 2020-07-13 12:09:44 +01:00
complex_io.cc Update copyright years. 2020-01-01 12:51:42 +01:00
concept-inst.cc Update copyright years. 2020-01-01 12:51:42 +01:00
cow-istream-string.cc Update copyright years. 2020-01-01 12:51:42 +01:00
ext-inst.cc Update copyright years. 2020-01-01 12:51:42 +01:00
globals_io.cc Update copyright years. 2020-01-01 12:51:42 +01:00
hash_tr1.cc Update copyright years. 2020-01-01 12:51:42 +01:00
hash-long-double-tr1-aux.cc Update copyright years. 2020-01-01 12:51:42 +01:00
hashtable_tr1.cc Update copyright years. 2020-01-01 12:51:42 +01:00
ios_failure.cc Update copyright years. 2020-01-01 12:51:42 +01:00
ios_init.cc Update copyright years. 2020-01-01 12:51:42 +01:00
ios_locale.cc Update copyright years. 2020-01-01 12:51:42 +01:00
istream-string.cc Update copyright years. 2020-01-01 12:51:42 +01:00
istream.cc libstdc++: Fix istream::ignore exit conditions (PR 94749, PR 96161) 2020-07-13 12:09:44 +01:00
list_associated-2.cc Update copyright years. 2020-01-01 12:51:42 +01:00
list_associated.cc Update copyright years. 2020-01-01 12:51:42 +01:00
list-aux-2.cc Update copyright years. 2020-01-01 12:51:42 +01:00
list-aux.cc Update copyright years. 2020-01-01 12:51:42 +01:00
list.cc Update copyright years. 2020-01-01 12:51:42 +01:00
locale_facets.cc Update copyright years. 2020-01-01 12:51:42 +01:00
locale_init.cc Update copyright years. 2020-01-01 12:51:42 +01:00
locale.cc Update copyright years. 2020-01-01 12:51:42 +01:00
localename.cc Update copyright years. 2020-01-01 12:51:42 +01:00
Makefile.am Update copyright years. 2020-01-01 12:51:42 +01:00
Makefile.in libstdc++: Regenerate makefiles 2020-06-23 07:59:40 +01:00
math_stubs_float.cc Update copyright years. 2020-01-01 12:51:42 +01:00
math_stubs_long_double.cc Update copyright years. 2020-01-01 12:51:42 +01:00
misc-inst.cc Update copyright years. 2020-01-01 12:51:42 +01:00
mt_allocator.cc Update copyright years. 2020-01-01 12:51:42 +01:00
parallel_settings.cc Update copyright years. 2020-01-01 12:51:42 +01:00
pool_allocator.cc Update copyright years. 2020-01-01 12:51:42 +01:00
stdexcept.cc Update copyright years. 2020-01-01 12:51:42 +01:00
streambuf.cc Update copyright years. 2020-01-01 12:51:42 +01:00
strstream.cc Update copyright years. 2020-01-01 12:51:42 +01:00
tree.cc Update copyright years. 2020-01-01 12:51:42 +01:00
valarray.cc Update copyright years. 2020-01-01 12:51:42 +01:00