From: Benjamin Kosnik Date: Fri, 24 May 2002 15:50:51 +0000 (+0000) Subject: re PR libstdc++/6750 (ofstream incorrectly sets failbit (severe regression)) X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=52f02d949e31180b80d2cd2535e9147bccceb901;p=gcc.git re PR libstdc++/6750 (ofstream incorrectly sets failbit (severe regression)) 2002-05-24 Benjamin Kosnik PR libstdc++/6750 * include/bits/ostream.tcc (ostream::operator<<(const char*)): Fix for empty string literal. (ostream::operator<<(const _CharT*)): Same. (ostream::operator<<(const char*)): Same. (ostream::operator<<(streambuf*)): Same. * testsuite/27_io/ostream_inserter_char.cc (test08): Add tests. * testsuite/27_io/ostream_inserter_other.cc (test02): Modify. From-SVN: r53839 --- diff --git a/libstdc++-v3/include/bits/ostream.tcc b/libstdc++-v3/include/bits/ostream.tcc index 550e405097c..71e4d888dac 100644 --- a/libstdc++-v3/include/bits/ostream.tcc +++ b/libstdc++-v3/include/bits/ostream.tcc @@ -119,19 +119,11 @@ namespace std basic_ostream<_CharT, _Traits>::operator<<(__streambuf_type* __sbin) { sentry __cerb(*this); - if (__cerb) + if (__cerb && __sbin) { try { - streamsize __xtrct = 0; - if (__sbin) - { - __streambuf_type* __sbout = this->rdbuf(); - __xtrct = __copy_streambufs(*this, __sbin, __sbout); - } - else - this->setstate(ios_base::badbit); - if (!__xtrct) + if (!__copy_streambufs(*this, __sbin, this->rdbuf())) this->setstate(ios_base::failbit); } catch(exception& __fail) @@ -143,6 +135,8 @@ namespace std __throw_exception_again; } } + else if (!__sbin) + this->setstate(ios_base::badbit); return *this; } @@ -539,14 +533,13 @@ namespace std { typedef basic_ostream<_CharT, _Traits> __ostream_type; typename __ostream_type::sentry __cerb(__out); - if (__cerb) + if (__cerb && __s) { try { streamsize __w = __out.width(); _CharT* __pads = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __w)); - streamsize __len = __s - ? static_cast(_Traits::length(__s)) : 0; + streamsize __len = static_cast(_Traits::length(__s)); if (__w > __len) { __pad(__out, __out.fill(), __pads, __s, __w, __len, false); @@ -555,8 +548,6 @@ namespace std } __out.write(__s, __len); __out.width(0); - if (!__len) - __out.setstate(ios_base::badbit); } catch(exception& __fail) { @@ -567,6 +558,8 @@ namespace std __throw_exception_again; } } + else if (!__s) + __out.setstate(ios_base::badbit); return __out; } @@ -581,9 +574,9 @@ namespace std typedef char_traits __traits_type; #endif typename __ostream_type::sentry __cerb(__out); - if (__cerb) + if (__cerb && __s) { - size_t __clen = __s ? __traits_type::length(__s) : 0; + size_t __clen = __traits_type::length(__s); _CharT* __ws = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * (__clen + 1))); for (size_t __i = 0; __i < __clen; ++__i) __ws[__i] = __out.widen(__s[__i]); @@ -603,8 +596,6 @@ namespace std } __out.write(__str, __len); __out.width(0); - if (!__len) - __out.setstate(ios_base::badbit); } catch(exception& __fail) { @@ -615,6 +606,8 @@ namespace std __throw_exception_again; } } + else if (!__s) + __out.setstate(ios_base::badbit); return __out; } @@ -625,14 +618,14 @@ namespace std { typedef basic_ostream __ostream_type; typename __ostream_type::sentry __cerb(__out); - if (__cerb) + if (__cerb && __s) { try { streamsize __w = __out.width(); char* __pads = static_cast(__builtin_alloca(__w)); - streamsize __len = __s ? - static_cast(_Traits::length(__s)) : 0; + streamsize __len = static_cast(_Traits::length(__s)); + if (__w > __len) { __pad(__out, __out.fill(), __pads, __s, __w, __len, false); @@ -641,8 +634,6 @@ namespace std } __out.write(__s, __len); __out.width(0); - if (!__len) - __out.setstate(ios_base::badbit); } catch(exception& __fail) { @@ -653,6 +644,8 @@ namespace std __throw_exception_again; } } + else if (!__s) + __out.setstate(ios_base::badbit); return __out; } diff --git a/libstdc++-v3/testsuite/27_io/ostream_inserter_char.cc b/libstdc++-v3/testsuite/27_io/ostream_inserter_char.cc index 7acaa02f542..2fe88f5815b 100644 --- a/libstdc++-v3/testsuite/27_io/ostream_inserter_char.cc +++ b/libstdc++-v3/testsuite/27_io/ostream_inserter_char.cc @@ -296,23 +296,35 @@ void test08() // 1 std::ostringstream oss; - oss << pt << std::endl; + oss << pt; VERIFY( oss.bad() ); VERIFY( oss.str().size() == 0 ); + oss.clear(); + oss << ""; + VERIFY( oss.good() ); + #if _GLIBCPP_USE_WCHAR_T // 2 std::wostringstream woss; - woss << pt << std::endl; + woss << pt; VERIFY( woss.bad() ); VERIFY( woss.str().size() == 0 ); + woss.clear(); + woss << ""; + VERIFY( woss.good() ); + // 3 wchar_t* wt = NULL; woss.clear(); - woss << wt << std::endl; + woss << wt; VERIFY( woss.bad() ); VERIFY( woss.str().size() == 0 ); + + woss.clear(); + woss << L""; + VERIFY( woss.good() ); #endif } diff --git a/libstdc++-v3/testsuite/27_io/ostream_inserter_other.cc b/libstdc++-v3/testsuite/27_io/ostream_inserter_other.cc index 2d030348ff3..4ce5b68d207 100644 --- a/libstdc++-v3/testsuite/27_io/ostream_inserter_other.cc +++ b/libstdc++-v3/testsuite/27_io/ostream_inserter_other.cc @@ -1,7 +1,7 @@ // 1999-08-16 bkoz // 1999-11-01 bkoz -// Copyright (C) 1999, 2000, 2001 Free Software Foundation +// Copyright (C) 1999, 2000, 2001, 2002 Free Software Foundation // // This file is part of the GNU ISO C++ Library. This library is free // software; you can redistribute it and/or modify it under the @@ -67,7 +67,7 @@ test02() f_out1 << strbuf01; state02 = f_out1.rdstate(); VERIFY( state01 != state02 ); - VERIFY( (state02 & std::ios_base::failbit) != 0 ); + VERIFY( (state02 & std::ios_base::badbit) != 0 ); // filebuf->filebuf std::ifstream f_in(name_01);