libstdc++: Avoid overflow in istream::get(streambuf&) [LWG 3464]
authorJonathan Wakely <jwakely@redhat.com>
Mon, 20 Jul 2020 19:06:46 +0000 (20:06 +0100)
committerJonathan Wakely <jwakely@redhat.com>
Mon, 20 Jul 2020 19:06:46 +0000 (20:06 +0100)
commit4d1c5b4957db2cb07f1053b7b87767275497d52e
tree177ba6f54bbfb42d2f020e0295ebf30ae0604107
parent138b1d4f58af17986e856e665ffbd561c2c8740e
libstdc++: Avoid overflow in istream::get(streambuf&) [LWG 3464]

Similar to the recent changes to basic_istream::ignore, this change
ensures that _M_gcount doesn't overflow when extracting characters and
inserting them into another streambuf.

The solution used here is to use unsigned long long for the count. We
assume that the number of characters extracted won't exceed the maximum
value for that type, but even if it does we avoid any undefined
behaviour.

libstdc++-v3/ChangeLog:

* include/bits/istream.tcc
(basic_istream::get(__streambuf_type&, char_type): Use unsigned
long long for counter and check if it would overflow _M_gcount.
* testsuite/27_io/basic_istream/get/char/lwg3464.cc: New test.
* testsuite/27_io/basic_istream/get/wchar_t/lwg3464.cc: New test.
libstdc++-v3/include/bits/istream.tcc
libstdc++-v3/testsuite/27_io/basic_istream/get/char/lwg3464.cc [new file with mode: 0644]
libstdc++-v3/testsuite/27_io/basic_istream/get/wchar_t/lwg3464.cc [new file with mode: 0644]