From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 9816538582A2; Mon, 11 Jul 2022 11:34:09 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9816538582A2 From: "redi at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug libstdc++/106248] [11/12/13 Regression] operator>>std::basic_istream at boundary condition behave differently in different opt levels Date: Mon, 11 Jul 2022 11:34:09 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: libstdc++ X-Bugzilla-Version: 11.2.0 X-Bugzilla-Keywords: wrong-code X-Bugzilla-Severity: normal X-Bugzilla-Who: redi at gcc dot gnu.org X-Bugzilla-Status: NEW X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: Message-ID: In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 11 Jul 2022 11:34:09 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D106248 --- Comment #4 from Jonathan Wakely --- To put it another way, in C++17 and earlier, writing the buffer stops becau= se we reach EOF when reading from the istream. If it *didn't* stop there, it w= ould overflow the buffer and have undefined behaviour. But luckily the program u= sed 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 st= ream 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 size= s to provide the safer C++20 behaviour, but that's only possible when optimizing= .=