https://gcc.gnu.org/bugzilla/show_bug.cgi?id=42857
--- Comment #10 from Jonathan Wakely <redi at gcc dot gnu.org> --- Untested patch: --- a/libstdc++-v3/src/c++98/compatibility.cc +++ b/libstdc++-v3/src/c++98/compatibility.cc @@ -88,7 +88,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { __sb->__safe_gbump(__size); _M_gcount += __size; - __c = __sb->sgetc(); + if (_M_gcount < __n + || __n == __gnu_cxx::__numeric_traits<streamsize>::__max) + __c = __sb->sgetc(); } else { A similar change would be needed in the other versions of this function (the wistream specialization, and the generic version).