[Bug libstdc++/106248] [11/12/13 Regression] operator>>std::basic_istream at boundary condition behave differently in different opt levels

redi at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Mon Jul 11 11:46:50 GMT 2022


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106248

--- Comment #6 from Jonathan Wakely <redi at gcc dot gnu.org> ---
I think this would restore the previous behaviour without losing the overflow
prevention:

--- a/libstdc++-v3/include/std/istream
+++ b/libstdc++-v3/include/std/istream
@@ -813,8 +813,17 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       else
        {
          if (__n == (size_t)-1)
-           __n = __gnu_cxx::__numeric_traits<streamsize>::__max;
-         std::__istream_extract(__in, __s, __n / sizeof(_CharT));
+           {
+             __n = __gnu_cxx::__numeric_traits<streamsize>::__max;
+             std::__istream_extract(__in, __s, __n / sizeof(_CharT));
+           }
+         else
+           {
+             std::__istream_extract(__in, __s, __n / sizeof(_CharT));
+             if (__in.good() && _Traits::eq_int_type(__in.rdbuf()->snextc(),
+                                                     _Traits::eof()))
+               __in.setstate(ios_base::eofbit);
+           }
        }
       return __in;
     }


More information about the Gcc-bugs mailing list