Bug 50501 - Insertion of complex into a stream may fail without invalidating the stream
Summary: Insertion of complex into a stream may fail without invalidating the stream
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: libstdc++ (show other bugs)
Version: 4.6.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-09-23 23:49 UTC by Christopher Yeleighton
Modified: 2011-09-24 00:35 UTC (History)
0 users

See Also:
Host: x86_64-suse-linux
Target: x86_64-suse-linux
Build: x86_64-suse-linux
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Christopher Yeleighton 2011-09-23 23:49:49 UTC
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.
Comment 1 Paolo Carlini 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.
Comment 2 Jonathan Wakely 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?