From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 8315 invoked by alias); 23 Sep 2011 23:50:11 -0000 Received: (qmail 8307 invoked by uid 22791); 23 Sep 2011 23:50:10 -0000 X-SWARE-Spam-Status: No, hits=-2.9 required=5.0 tests=ALL_TRUSTED,AWL,BAYES_00 X-Spam-Check-By: sourceware.org Received: from localhost (HELO gcc.gnu.org) (127.0.0.1) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Fri, 23 Sep 2011 23:49:56 +0000 From: "giecrilj at stegny dot 2a.pl" To: gcc-bugs@gcc.gnu.org Subject: [Bug libstdc++/50501] New: Insertion of complex into a stream may fail without invalidating the stream Date: Sat, 24 Sep 2011 00:35:00 -0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: libstdc++ X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: giecrilj at stegny dot 2a.pl X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Changed-Fields: Message-ID: X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated Content-Type: text/plain; charset="UTF-8" MIME-Version: 1.0 Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-bugs-owner@gcc.gnu.org X-SW-Source: 2011-09/txt/msg01729.txt.bz2 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50501 Bug #: 50501 Summary: Insertion of complex into a stream may fail without invalidating the stream Classification: Unclassified Product: gcc Version: 4.6.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: libstdc++ AssignedTo: unassigned@gcc.gnu.org ReportedBy: giecrilj@stegny.2a.pl Host: x86_64-suse-linux Target: x86_64-suse-linux Build: x86_64-suse-linux I can see the following implementation in : /// Insertion operator for complex values. template basic_ostream<_CharT, _Traits>& operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x) { basic_ostringstream<_CharT, _Traits> __s; __s.flags(__os.flags()); __s.imbue(__os.getloc()); __s.precision(__os.precision()); __s << '(' << __x.real() << ',' << __x.imag() << ')'; // This can fail! return __os << __s.str(); // And then this may be empty! } This implementation may cause the following test case to fail: #include #include #include #include int main () { float const a_f ((01)); ::std:: complex < float > a_c ((a_f)); std:: stringstream a_s; assert (!(a_s << a_c) || ((a_c = float ()) == float () && a_s >> a_c && a_c == a_f)); return +EXIT_SUCCESS; } I cannot reproduce the failure because I do not know how to inject a memory allocation failure. However, I imagine it is possible. I suggest that the implementation be modified: /// Insertion operator for complex values. template basic_ostream<_CharT, _Traits>& operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x) { basic_ostringstream<_CharT, _Traits> __s; __s.flags(__os.flags()); __s.imbue(__os.getloc()); __s.precision(__os.precision()); __s << '(' << __x.real() << ',' << __x.imag() << ')'; __os. setstate(__s.rdstate()) ; return __os << __s.str(); } Thank you for your consideration.