From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 3645538845A3; Thu, 13 Jun 2024 17:17:07 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3645538845A3 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1718299027; bh=C/PVNy/zBGaqdAcucnAUV02R/TMKp3ssfWTu37dPu+c=; h=From:To:Subject:Date:In-Reply-To:References:From; b=JFKaLfb/IFolaHOk9Bw/GBOa8irh7PuyhzRWVHm6HjqRRD+iQdZTo4d7KnhBG+Was hrIcmtXoRB9dUolbOrECPQeAjpzp0WTFt8QAhTmFgqezp7crWdF97hTq2ExgLBPK5N 3b3YxWCebEt+8FRQ/xmHrb2OOm5xU1dSkZ9Oriyg= 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 17:17:06 +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 #7 from Jonathan Wakely --- I think we can just do this in __ostream_insert: --- a/libstdc++-v3/include/bits/ostream_insert.h +++ b/libstdc++-v3/include/bits/ostream_insert.h @@ -103,6 +103,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION else __ostream_write(__out, __s, __n); __out.width(0); + + if (__builtin_expect(__out.bad(), 0)) + __out.setstate(__ios_base::failbit); } __catch(__cxxabiv1::__forced_unwind&) { If __ostream_write or __ostream_fill fails to insert something (as unformat= ted output functions) they'll set badbit. So we can check for that and set fail= bit explicitly, to meet the requirement for a formatted output function.=