public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/50501] New: Insertion of complex into a stream may fail without invalidating the stream
@ 2011-09-24  0:35 giecrilj at stegny dot 2a.pl
  2011-09-24  4:12 ` [Bug libstdc++/50501] " paolo.carlini at oracle dot com
  2011-09-24  4:25 ` redi at gcc dot gnu.org
  0 siblings, 2 replies; 3+ messages in thread
From: giecrilj at stegny dot 2a.pl @ 2011-09-24  0:35 UTC (permalink / raw)
  To: gcc-bugs

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 <complex>:

///  Insertion operator for complex values.
  template<typename _Tp, typename _CharT, class _Traits>
    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 <cstdlib>
#include <cassert>
#include <iostream>
#include <complex>

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<typename _Tp, typename _CharT, class _Traits>
    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.


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [Bug libstdc++/50501] Insertion of complex into a stream may fail without invalidating the stream
  2011-09-24  0:35 [Bug libstdc++/50501] New: Insertion of complex into a stream may fail without invalidating the stream giecrilj at stegny dot 2a.pl
@ 2011-09-24  4:12 ` paolo.carlini at oracle dot com
  2011-09-24  4:25 ` redi at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: paolo.carlini at oracle dot com @ 2011-09-24  4:12 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50501

Paolo Carlini <paolo.carlini at oracle dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |INVALID

--- Comment #1 from Paolo Carlini <paolo.carlini at oracle dot com> 2011-09-23 23:58:54 UTC ---
The Standard is very precise here, and nothing changed for C++11. It says,
literally:

Effects: inserts the complex number x onto the stream o as if it were
implemented as follows:

template<class T, class charT, class traits>
basic_ostream<charT, traits>&
operator<<(basic_ostream<charT, traits>& o, const complex<T>& x) {
  basic_ostringstream<charT, traits> s;
  s.flags(o.flags());
  s.imbue(o.getloc());
  s.precision(o.precision());
  s << ’(’ << x.real() << "," << x.imag() << ’)’;
  return o << s.str();
}

thus, I don't see how we could to something else, like what you are suggesting,
and remain conforming.


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [Bug libstdc++/50501] Insertion of complex into a stream may fail without invalidating the stream
  2011-09-24  0:35 [Bug libstdc++/50501] New: Insertion of complex into a stream may fail without invalidating the stream giecrilj at stegny dot 2a.pl
  2011-09-24  4:12 ` [Bug libstdc++/50501] " paolo.carlini at oracle dot com
@ 2011-09-24  4:25 ` redi at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: redi at gcc dot gnu.org @ 2011-09-24  4:25 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50501

--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> 2011-09-24 00:35:13 UTC ---
What failure scenario are you talking about?
Surely if there's a memory allocation failure it will throw a bad_alloc
exception, so __s.str() will never get called?


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2011-09-24  0:35 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-09-24  0:35 [Bug libstdc++/50501] New: Insertion of complex into a stream may fail without invalidating the stream giecrilj at stegny dot 2a.pl
2011-09-24  4:12 ` [Bug libstdc++/50501] " paolo.carlini at oracle dot com
2011-09-24  4:25 ` redi at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).