From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 941423882060; Thu, 13 Jun 2024 16:45:48 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 941423882060 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1718297148; bh=v8NnZTszVC8Na/d0CFViQwm1aJoYonF/xjlQFcARvrM=; h=From:To:Subject:Date:In-Reply-To:References:From; b=lTo5w7PexmGr0YV2YBOR8InSHB6Ei3Nqz08gMesu/DgT4+C4yzmoAf9mnq5Px1XKr BSxAg50MaGwUvVepOFRAFmzUaGEGflahjZ90NPFE5TaRRgSSCw9MttUqKwNDrbnKfc VBEBUrR3vNOembeL3JkgoE4U7MMWfPAtv2G7Rs3s= From: "redi at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug libstdc++/28103] std::operator<<(ostream&, string) sets badbit instead of failbit on failure Date: Thu, 13 Jun 2024 16:45:47 +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: 4.1.0 X-Bugzilla-Keywords: 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: paolo.carlini at oracle dot com 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 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D28103 --- Comment #6 from Jonathan Wakely --- It looks like we've been setting badbit in ostream::_M_write(const charT*, streamsize) since it was added in r0-47606-g8d0a564bba54f7 That function was used to replace the similar code in basic_ostream::write: streamsize __put =3D this->rdbuf()->sputn(__s, __n); if ( __put !=3D __n) this->setstate(ios_base::badbit); and in the operator<< for std::basic_string: streamsize __res =3D __out.rdbuf()->sputn(__s, __len); __out.width(0); if (__res !=3D __len) __out.setstate(ios_base::failbit); However that created the problem, because basic_ostream::write is indeed specified to set badbit but the string inserter sets failbit. [ostream.unformatted] says: "inserting in the output sequence fails (in which case the function calls setstate(badbit)" [string.io] defers to [string.view.io] which defers to [ostream.formatted] which says: "If the generation fails, then the formatted output function does setstate(ios_base::failbit)" So we have different requirements for ostream::write and string insertion, = but we implement them both the same way since r0-47606-g8d0a564bba54f7 (the _M_write member was later replaced by __ostream_write but that uses the sa= me logic and sets badbit).=