https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106248
--- Comment #4 from Jonathan Wakely <redi at gcc dot gnu.org> --- To put it another way, in C++17 and earlier, writing the buffer stops because we reach EOF when reading from the istream. If it *didn't* stop there, it would overflow the buffer and have undefined behaviour. But luckily the program used the correct buffer size, and EOF happens at just the right time to prevent overflow. This is the behaviour required by the C++17 standard. In C++20, writing to the buffer stops because we know the buffer is full and will not allow it to overflow. That means we don't detect that the input stream reached EOF, because we don't even try to read any more characters. This is the behaviour required by the C++20 standard. The "bug" is that for C++17 mode we use GCC's ability to detect buffer sizes to provide the safer C++20 behaviour, but that's only possible when optimizing.