aboutsummaryrefslogtreecommitdiff
path: root/libstdc++-v3/src
diff options
context:
space:
mode:
authorJonathan Wakely <jwakely@redhat.com>2024-04-04 10:33:33 +0100
committerJonathan Wakely <jwakely@redhat.com>2024-04-15 19:26:09 +0100
commit2d694414ada8e3b58f504c1b175d31088529632e (patch)
treed965cfa433ae2da665ee22f7599439640e9f7d88 /libstdc++-v3/src
parent6e11bb451babfe47bb6b7ad42335019f2771a32e (diff)
downloadgcc-2d694414ada8e3b58f504c1b175d31088529632e.zip
gcc-2d694414ada8e3b58f504c1b175d31088529632e.tar.gz
gcc-2d694414ada8e3b58f504c1b175d31088529632e.tar.bz2
libstdc++: Fix infinite loop in std::istream::ignore(n, delim) [PR93672]
A negative delim value passed to std::istream::ignore can never match any character in the stream, because the comparison is done using traits_type::eq_int_type(sb->sgetc(), delim) and sgetc() never returns negative values (except at EOF). The optimized version of ignore for the std::istream specialization uses traits_type::find to locate the delim character in the streambuf, which _can_ match a negative delim on platforms where char is signed, but then we do another comparison using eq_int_type which fails. The code then keeps looping forever, with traits_type::find locating the character and traits_type::eq_int_type saying it's not a match, so traits_type::find is used again and finds the same character again. A possible fix would be to check with eq_int_type after a successful find, to see whether we really have a match. However, that would be suboptimal since we know that a negative delimiter will never match using eq_int_type. So a better fix is to adjust the check at the top of the function that handles delim==eof(), so that we treat all negative delim values as equivalent to EOF. That way we don't bother using find to search for something that will never match with eq_int_type. The version of ignore in the primary template doesn't need a change, because it doesn't use traits_type::find, instead characters are extracted one-by-one and always matched using eq_int_type. That avoids the inconsistency between find and eq_int_type. The specialization for std::wistream does use traits_type::find, but traits_type::to_int_type is equivalent to an implicit conversion from wchar_t to wint_t, so passing a wchar_t directly to ignore without using to_int_type works. libstdc++-v3/ChangeLog: PR libstdc++/93672 * src/c++98/istream.cc (istream::ignore(streamsize, int_type)): Treat all negative delimiter values as eof(). * testsuite/27_io/basic_istream/ignore/char/93672.cc: New test. * testsuite/27_io/basic_istream/ignore/wchar_t/93672.cc: New test.
Diffstat (limited to 'libstdc++-v3/src')
-rw-r--r--libstdc++-v3/src/c++98/istream.cc13
1 files changed, 11 insertions, 2 deletions
diff --git a/libstdc++-v3/src/c++98/istream.cc b/libstdc++-v3/src/c++98/istream.cc
index 07ac739..d1b4444 100644
--- a/libstdc++-v3/src/c++98/istream.cc
+++ b/libstdc++-v3/src/c++98/istream.cc
@@ -112,8 +112,17 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
basic_istream<char>::
ignore(streamsize __n, int_type __delim)
{
- if (traits_type::eq_int_type(__delim, traits_type::eof()))
- return ignore(__n);
+ {
+ // If conversion to int_type changes the value then __delim does not
+ // correspond to a value of type char_type, and so will never match
+ // a character extracted from the input sequence. Just use ignore(n).
+ const int_type chk_delim = traits_type::to_int_type(__delim);
+ const bool matchable = traits_type::eq_int_type(chk_delim, __delim);
+ if (__builtin_expect(!matchable, 0))
+ return ignore(__n);
+ // Now we know that __delim is a valid char_type value, so it's safe
+ // for the code below to use traits_type::find to search for it.
+ }
_M_gcount = 0;
sentry __cerb(*this, true);