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

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|                            |2022-07-11
             Status|UNCONFIRMED                 |NEW
            Summary|operator>>std::basic_istrea |[11/12/13 Regression]
                   |m at boundary condition     |operator>>std::basic_istrea
                   |behave differently in       |m at boundary condition
                   |different opt levels        |behave differently in
                   |                            |different opt levels
      Known to fail|                            |11.1.0
     Ever confirmed|0                           |1
      Known to work|                            |10.4.0

--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> ---
In C++20 mode the operator>> overload has changed to one that binds to char
(&)[10] and so knows the size of the output buffer. The loop stops reading when
it has written as many chars as will fit in the buffer, and does not set
eofbit:

              if (__extracted < __num - 1
                  && __traits_type::eq_int_type(__c, __eof))
                __err |= ios_base::eofbit;

In pre-C++20 modes, operator>> just binds to a const char* and will overflow it
if the buffer is not big enough. However, libstdc++ now uses
__builtin_object_size to detect the size of the buffer, and so will stop
writing when the buffer is full. That detection using __builtin_object_size
only works when optimization is enabled, which is why we stop before setting
the eofbit when optimizing.

I don't want to just remove the size detection, because it prevents undefined
behaviour. But we need a way to prevent overflow without altering the
observable behaviour for C++17 and earlier.

Reply via email to