2002-05-24 Benjamin Kosnik <bkoz@redhat.com>
authorbkoz <bkoz@138bc75d-0d04-0410-961f-82ee72b054a4>
Fri, 24 May 2002 15:50:51 +0000 (15:50 +0000)
committerbkoz <bkoz@138bc75d-0d04-0410-961f-82ee72b054a4>
Fri, 24 May 2002 15:50:51 +0000 (15:50 +0000)
PR libstdc++/6750
* include/bits/ostream.tcc (ostream::operator<<(const char*)): Fix
for empty string literal.
(ostream::operator<<(const _CharT*)): Same.
(ostream<char>::operator<<(const char*)): Same.
(ostream<char>::operator<<(streambuf*)): Same.
* testsuite/27_io/ostream_inserter_char.cc (test08): Add tests.
* testsuite/27_io/ostream_inserter_other.cc (test02): Modify.

git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@53839 138bc75d-0d04-0410-961f-82ee72b054a4

libstdc++-v3/include/bits/ostream.tcc
libstdc++-v3/testsuite/27_io/ostream_inserter_char.cc
libstdc++-v3/testsuite/27_io/ostream_inserter_other.cc

index 550e405..71e4d88 100644 (file)
@@ -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<streamsize>(_Traits::length(__s)) : 0;
+             streamsize __len = static_cast<streamsize>(_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<char>                     __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<char, _Traits> __ostream_type;
       typename __ostream_type::sentry __cerb(__out);
-      if (__cerb)
+      if (__cerb && __s)
        {
          try 
            {
              streamsize __w = __out.width();
              char* __pads = static_cast<char*>(__builtin_alloca(__w));
-             streamsize __len = __s ? 
-                            static_cast<streamsize>(_Traits::length(__s)) : 0;
+             streamsize __len = static_cast<streamsize>(_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;
     }
 
index 7acaa02..2fe88f5 100644 (file)
@@ -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
 }
 
index 2d03034..4ce5b68 100644 (file)
@@ -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);