From 7bd9b6d491a8ecae045944f6eeba77a98e82d0c1 Mon Sep 17 00:00:00 2001 From: Benjamin Kosnik Date: Wed, 15 May 2002 14:38:30 +0000 Subject: [PATCH] re PR libstdc++/6518 (???) 2002-05-15 Benjamin Kosnik PR libstdc++/6518 * include/bits/ostream.tcc (ostream::operator<<(const char*)): Fix for null case. (ostream::operator<<(const _CharT*)): Same. (ostream::operator<<(const char*)): Same. * testsuite/27_io/ostream_inserter_char.cc (test07): Add test. From-SVN: r53489 --- libstdc++-v3/ChangeLog | 9 ++++++ libstdc++-v3/include/bits/ostream.tcc | 18 ++++++++---- .../testsuite/27_io/ostream_inserter_char.cc | 28 +++++++++++++++++++ 3 files changed, 50 insertions(+), 5 deletions(-) diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 40181511bb0..b1bb70159c7 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,12 @@ +2002-05-15 Benjamin Kosnik + + PR libstdc++/6518 + * include/bits/ostream.tcc (ostream::operator<<(const char*)): Fix + for null case. + (ostream::operator<<(const _CharT*)): Same. + (ostream::operator<<(const char*)): Same. + * testsuite/27_io/ostream_inserter_char.cc (test07): Add test. + 2002-05-15 Benjamin Kosnik PR libstdc++/6594 diff --git a/libstdc++-v3/include/bits/ostream.tcc b/libstdc++-v3/include/bits/ostream.tcc index d15b9f2afa3..550e405097c 100644 --- a/libstdc++-v3/include/bits/ostream.tcc +++ b/libstdc++-v3/include/bits/ostream.tcc @@ -545,7 +545,8 @@ namespace std { streamsize __w = __out.width(); _CharT* __pads = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __w)); - streamsize __len = static_cast(_Traits::length(__s)); + streamsize __len = __s + ? static_cast(_Traits::length(__s)) : 0; if (__w > __len) { __pad(__out, __out.fill(), __pads, __s, __w, __len, false); @@ -554,6 +555,8 @@ namespace std } __out.write(__s, __len); __out.width(0); + if (!__len) + __out.setstate(ios_base::badbit); } catch(exception& __fail) { @@ -575,14 +578,14 @@ namespace std #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS // 167. Improper use of traits_type::length() // Note that this is only in 'Review' status. - typedef char_traits __ctraits_type; + typedef char_traits __traits_type; #endif typename __ostream_type::sentry __cerb(__out); if (__cerb) { - size_t __clen = __ctraits_type::length(__s); + 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) + for (size_t __i = 0; __i < __clen; ++__i) __ws[__i] = __out.widen(__s[__i]); _CharT* __str = __ws; @@ -600,6 +603,8 @@ namespace std } __out.write(__str, __len); __out.width(0); + if (!__len) + __out.setstate(ios_base::badbit); } catch(exception& __fail) { @@ -626,7 +631,8 @@ namespace std { streamsize __w = __out.width(); char* __pads = static_cast(__builtin_alloca(__w)); - streamsize __len = static_cast(_Traits::length(__s)); + streamsize __len = __s ? + static_cast(_Traits::length(__s)) : 0; if (__w > __len) { __pad(__out, __out.fill(), __pads, __s, __w, __len, false); @@ -635,6 +641,8 @@ namespace std } __out.write(__s, __len); __out.width(0); + if (!__len) + __out.setstate(ios_base::badbit); } catch(exception& __fail) { diff --git a/libstdc++-v3/testsuite/27_io/ostream_inserter_char.cc b/libstdc++-v3/testsuite/27_io/ostream_inserter_char.cc index e8c15588557..7acaa02f542 100644 --- a/libstdc++-v3/testsuite/27_io/ostream_inserter_char.cc +++ b/libstdc++-v3/testsuite/27_io/ostream_inserter_char.cc @@ -289,6 +289,33 @@ void test07() #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(); @@ -298,5 +325,6 @@ int main() test05(); test06(); test07(); + test08(); return 0; }