This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[v3] libstdc++/6518
- From: Benjamin Kosnik <bkoz at redhat dot com>
- To: gcc-patches at gcc dot gnu dot org
- Date: Wed, 15 May 2002 07:35:19 -0700
- Subject: [v3] libstdc++/6518
Don't core on undefined behavior.
tested x86/linux
2002-05-15 Benjamin Kosnik <bkoz@redhat.com>
PR libstdc++/6518
* include/bits/ostream.tcc (ostream::operator<<(const char*)): Fix
for null case.
(ostream::operator<<(const _CharT*)): Same.
(ostream<char>::operator<<(const char*)): Same.
* testsuite/27_io/ostream_inserter_char.cc (test07): Add test.
Index: include/bits/ostream.tcc
===================================================================
RCS file: /cvs/gcc/gcc/libstdc++-v3/include/bits/ostream.tcc,v
retrieving revision 1.27
diff -c -p -r1.27 ostream.tcc
*** include/bits/ostream.tcc 12 Apr 2002 05:42:23 -0000 1.27
--- include/bits/ostream.tcc 15 May 2002 14:34:09 -0000
*************** namespace std
*** 545,551 ****
{
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);
--- 545,552 ----
{
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);
*************** namespace std
*** 554,559 ****
--- 555,562 ----
}
__out.write(__s, __len);
__out.width(0);
+ if (!__len)
+ __out.setstate(ios_base::badbit);
}
catch(exception& __fail)
{
*************** namespace std
*** 575,588 ****
#ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
// 167. Improper use of traits_type::length()
// Note that this is only in 'Review' status.
! typedef char_traits<char> __ctraits_type;
#endif
typename __ostream_type::sentry __cerb(__out);
if (__cerb)
{
! size_t __clen = __ctraits_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;
--- 578,591 ----
#ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
// 167. Improper use of traits_type::length()
// Note that this is only in 'Review' status.
! typedef char_traits<char> __traits_type;
#endif
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;
*************** namespace std
*** 600,605 ****
--- 603,610 ----
}
__out.write(__str, __len);
__out.width(0);
+ if (!__len)
+ __out.setstate(ios_base::badbit);
}
catch(exception& __fail)
{
*************** namespace std
*** 626,632 ****
{
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);
--- 631,638 ----
{
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);
*************** namespace std
*** 635,640 ****
--- 641,648 ----
}
__out.write(__s, __len);
__out.width(0);
+ if (!__len)
+ __out.setstate(ios_base::badbit);
}
catch(exception& __fail)
{
Index: testsuite/27_io/ostream_inserter_char.cc
===================================================================
RCS file: /cvs/gcc/gcc/libstdc++-v3/testsuite/27_io/ostream_inserter_char.cc,v
retrieving revision 1.8
diff -c -p -r1.8 ostream_inserter_char.cc
*** testsuite/27_io/ostream_inserter_char.cc 25 Jan 2002 06:36:32 -0000 1.8
--- testsuite/27_io/ostream_inserter_char.cc 15 May 2002 14:34:11 -0000
*************** void test07()
*** 289,294 ****
--- 289,321 ----
#endif
}
+ void test08()
+ {
+ bool test = true;
+ char* pt = NULL;
+
+ // 1
+ std::ostringstream oss;
+ oss << pt << std::endl;
+ VERIFY( oss.bad() );
+ VERIFY( oss.str().size() == 0 );
+
+ #if _GLIBCPP_USE_WCHAR_T
+ // 2
+ std::wostringstream woss;
+ woss << pt << std::endl;
+ VERIFY( woss.bad() );
+ VERIFY( woss.str().size() == 0 );
+
+ // 3
+ wchar_t* wt = NULL;
+ woss.clear();
+ woss << wt << std::endl;
+ VERIFY( woss.bad() );
+ VERIFY( woss.str().size() == 0 );
+ #endif
+ }
+
int main()
{
test01();
*************** int main()
*** 298,302 ****
--- 325,330 ----
test05();
test06();
test07();
+ test08();
return 0;
}