[PATCH] Fix libstdc++/6750

Paolo Carlini pcarlini@unitus.it
Fri May 24 08:57:00 GMT 2002


Hi,

the following patch, which is the result of the gcc-patches thread on 
libstdc++/6518, fixes libstdc++/6750 while not breaking the fix for 
libstdc++/6518.

We check __s for NULL at the beginning of each operator<< and in that 
case we set the badbit, otherwise everything goes on as before the fix 
for libstdc++/6518, that is we can output correctly also a zero length 
string.

I'm a little bit unsure about the __out.width(0) call: in this patch we 
do it also if __s == NULL, but I can change this if you think it would 
be a more conforming behaviour.

Tested i686-pc-linux-gnu (also tested the original 6750 testcase 
involving getline)

Ok for mainline and branch?

Ciao,
Paolo.

//////////////

2002-05-23  Paolo Carlini  <pcarlini@unitus.it>
            Jakub Jelinek  <jakub@redhat.com>

    PR libstdc++/6750
    * include/bits/ostream.tcc (ostream::operator<<(const char*)):
    Fix for zero length strings.
    (ostream::operator<<(const _CharT*)): Likewise.
    (ostream<char>::operator<<(const char*)): Likewise.
    * testsuite/27_io/ostream_inserter_char.cc: Add test09.

diff -prN libstdc++-v3-orig/include/bits/ostream.tcc 
libstdc++-v3/include/bits/ostream.tcc
*** libstdc++-v3-orig/include/bits/ostream.tcc    Fri May 17 19:59:02 2002
--- libstdc++-v3/include/bits/ostream.tcc    Thu May 23 21:41:58 2002
*************** namespace std
*** 475,481 ****
        try
          {
            streamsize __w = __out.width();
!           _CharT* __pads = 
static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * (__w + 1)));
            __pads[0] = __c;
            streamsize __len = 1;
            if (__w > __len)
--- 475,482 ----
        try
          {
            streamsize __w = __out.width();
!           _CharT* __pads =
!         static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * (__w + 
1)));
            __pads[0] = __c;
            streamsize __len = 1;
            if (__w > __len)
*************** namespace std
*** 541,571 ****
        typename __ostream_type::sentry __cerb(__out);
        if (__cerb)
      {
!       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;
-           if (__w > __len)
-         {
-           __pad(__out, __out.fill(), __pads, __s, __w, __len, false);
-           __s = __pads;
-           __len = __w;
-         }
-           __out.write(__s, __len);
            __out.width(0);
-           if (!__len)
-         __out.setstate(ios_base::badbit);
-         }
-       catch(exception& __fail)
-         {
-           // 27.6.1.2.1 Common requirements.
-           // Turn this on without causing an ios::failure to be thrown.
            __out.setstate(ios_base::badbit);
-           if ((__out.exceptions() & ios_base::badbit) != 0)
-         __throw_exception_again;
          }
      }
        return __out;
      }
--- 542,576 ----
        typename __ostream_type::sentry __cerb(__out);
        if (__cerb)
      {
!       if (!__s)
          {
            __out.width(0);
            __out.setstate(ios_base::badbit);
          }
+       else
+         try
+           {
+         streamsize __w = __out.width();
+         _CharT* __pads =
+           static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __w));
+         streamsize __len = static_cast<streamsize>(_Traits::length(__s));
+         if (__w > __len)
+           {
+             __pad(__out, __out.fill(), __pads, __s, __w, __len, false);
+             __s = __pads;
+             __len = __w;
+           }
+           __out.write(__s, __len);
+           __out.width(0);
+           }
+         catch(exception& __fail)
+           {
+         // 27.6.1.2.1 Common requirements.
+         // Turn this on without causing an ios::failure to be thrown.
+         __out.setstate(ios_base::badbit);
+         if ((__out.exceptions() & ios_base::badbit) != 0)
+           __throw_exception_again;
+           }
      }
        return __out;
      }
*************** namespace std
*** 583,618 ****
        typename __ostream_type::sentry __cerb(__out);
        if (__cerb)
      {
!       size_t __clen = __s ? __traits_type::length(__s) : 0;
!       _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]);
!       _CharT* __str = __ws;
!      
!       try
          {
-           streamsize __len = static_cast<streamsize>(__clen);
-           streamsize __w = __out.width();
-           _CharT* __pads = 
static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __w));
-          
-           if (__w > __len)
-         {
-           __pad(__out, __out.fill(), __pads, __ws, __w, __len, false);
-           __str = __pads;
-           __len = __w;
-         }
-           __out.write(__str, __len);
            __out.width(0);
!           if (!__len)
!         __out.setstate(ios_base::badbit);
          }
!       catch(exception& __fail)
          {
!           // 27.6.1.2.1 Common requirements.
!           // Turn this on without causing an ios::failure to be thrown.
!           __out.setstate(ios_base::badbit);
!           if ((__out.exceptions() & ios_base::badbit) != 0)
!         __throw_exception_again;
          }
      }
        return __out;
--- 588,629 ----
        typename __ostream_type::sentry __cerb(__out);
        if (__cerb)
      {
!       if (!__s)
          {
            __out.width(0);
!           __out.setstate(ios_base::badbit);
          }
!       else
          {
!           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]);
!           _CharT* __str = __ws;
!           try
!         {
!           streamsize __len = static_cast<streamsize>(__clen);
!           streamsize __w = __out.width();
!           _CharT* __pads =
!             static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __w));
!           if (__w > __len)
!             {
!               __pad(__out, __out.fill(), __pads, __ws, __w, __len, false);
!               __str = __pads;
!               __len = __w;
!             }
!           __out.write(__str, __len);
!           __out.width(0);
!         }
!           catch(exception& __fail)
!         {
!           // 27.6.1.2.1 Common requirements.
!           // Turn this on without causing an ios::failure to be thrown.
!           __out.setstate(ios_base::badbit);
!           if ((__out.exceptions() & ios_base::badbit) != 0)
!             __throw_exception_again;
!         }
          }
      }
        return __out;
*************** namespace std
*** 627,657 ****
        typename __ostream_type::sentry __cerb(__out);
        if (__cerb)
      {
!       try
          {
-           streamsize __w = __out.width();
-           char* __pads = static_cast<char*>(__builtin_alloca(__w));
-           streamsize __len = __s ?
-                      static_cast<streamsize>(_Traits::length(__s)) : 0;
-           if (__w > __len)
-         {
-           __pad(__out, __out.fill(), __pads, __s, __w, __len, false);
-           __s = __pads;
-           __len = __w;
-         }
-           __out.write(__s, __len);
            __out.width(0);
-           if (!__len)
-         __out.setstate(ios_base::badbit);
-         }
-       catch(exception& __fail)
-         {
-           // 27.6.1.2.1 Common requirements.
-           // Turn this on without causing an ios::failure to be thrown.
            __out.setstate(ios_base::badbit);
-           if ((__out.exceptions() & ios_base::badbit) != 0)
-         __throw_exception_again;
          }
      }
        return __out;
      }
--- 638,671 ----
        typename __ostream_type::sentry __cerb(__out);
        if (__cerb)
      {
!       if (!__s)
          {
            __out.width(0);
            __out.setstate(ios_base::badbit);
          }
+       else
+         try
+           {
+         streamsize __w = __out.width();
+         char* __pads = static_cast<char*>(__builtin_alloca(__w));
+         streamsize __len = static_cast<streamsize>(_Traits::length(__s));
+         if (__w > __len)
+           {
+             __pad(__out, __out.fill(), __pads, __s, __w, __len, false);
+             __s = __pads;
+             __len = __w;
+           }
+           __out.write(__s, __len);
+           __out.width(0);
+           }
+         catch(exception& __fail)
+           {
+         // 27.6.1.2.1 Common requirements.
+         // Turn this on without causing an ios::failure to be thrown.
+         __out.setstate(ios_base::badbit);
+         if ((__out.exceptions() & ios_base::badbit) != 0)
+           __throw_exception_again;
+           }
      }
        return __out;
      }
diff -prN libstdc++-v3-orig/testsuite/27_io/ostream_inserter_char.cc 
libstdc++-v3/testsuite/27_io/ostream_inserter_char.cc
*** libstdc++-v3-orig/testsuite/27_io/ostream_inserter_char.cc    Fri 
May 17 19:59:05 2002
--- libstdc++-v3/testsuite/27_io/ostream_inserter_char.cc    Thu May 23 
21:27:34 2002
*************** void test08()
*** 316,321 ****
--- 316,348 ----
  #endif
  }
 
+ // libstdc++/6750
+ void test09()
+ {
+   bool test = true;
+   char* pt = "";
+
+   // 1
+   std::ostringstream oss;
+   oss << pt;
+   VERIFY( oss.good() );
+   VERIFY( oss.str().size() == 0 );
+
+ #if _GLIBCPP_USE_WCHAR_T
+   // 2
+   std::wostringstream woss;
+   woss << pt;
+   VERIFY( woss.good() );
+   VERIFY( woss.str().size() == 0 );
+
+   // 3
+   wchar_t* wt = L"";
+   woss << wt;
+   VERIFY( woss.good() );
+   VERIFY( woss.str().size() == 0 );
+ #endif
+ }
+
  int main()
  {
    test01();
*************** int main()
*** 326,330 ****
--- 353,358 ----
    test06();
    test07();
    test08();
+   test09();
    return 0;
  }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: patch_6750
Type: application/x-java-vm
Size: 8070 bytes
Desc: not available
URL: <http://gcc.gnu.org/pipermail/libstdc++/attachments/20020524/2040d372/attachment.bin>


More information about the Libstdc++ mailing list