From fcaa810173fdf3915198350893f1bb90aa7cb689 Mon Sep 17 00:00:00 2001 From: Benjamin Kosnik Date: Tue, 12 Mar 2002 22:10:34 +0000 Subject: [PATCH] basic_string.tcc (string::_S_construct(_InIter, _InIter, const _Alloc&, forward_iterator_tag): Check for null. 2002-03-12 Benjamin Kosnik * include/bits/basic_string.tcc (string::_S_construct(_InIter, _InIter, const _Alloc&, forward_iterator_tag): Check for null. (string::basic_string(const _CharT* __s, const _Alloc& __a)): Same. * testsuite/21_strings/ctor_copy_dtor.cc (test01): Re-enable, now that memory limits are in place. (test03): Add tests. From-SVN: r50683 --- libstdc++-v3/ChangeLog | 9 ++++ libstdc++-v3/include/bits/basic_string.tcc | 7 +++- .../testsuite/21_strings/ctor_copy_dtor.cc | 41 +++++++++++-------- 3 files changed, 39 insertions(+), 18 deletions(-) diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 078d795507e..9e07ffd9419 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,12 @@ +2002-03-12 Benjamin Kosnik + + * include/bits/basic_string.tcc (string::_S_construct(_InIter, + _InIter, const _Alloc&, forward_iterator_tag): Check for null. + (string::basic_string(const _CharT* __s, const _Alloc& __a)): Same. + * testsuite/21_strings/ctor_copy_dtor.cc (test01): Re-enable, now + that memory limits are in place. + (test03): Add tests. + 2002-03-11 Benjamin Kosnik * acinclude.m4 (GLIBCPP_CHECK_WCHAR_T_SUPPORT): Check for wctype.h. diff --git a/libstdc++-v3/include/bits/basic_string.tcc b/libstdc++-v3/include/bits/basic_string.tcc index 01a3fe6ea8c..dc6db6e1bcb 100644 --- a/libstdc++-v3/include/bits/basic_string.tcc +++ b/libstdc++-v3/include/bits/basic_string.tcc @@ -139,6 +139,10 @@ namespace std { size_type __dnew = static_cast(distance(__beg, __end)); + // NB: Not required, but considered best practice. + if (__builtin_expect(__beg == _InIter(0), 0)) + __throw_logic_error("attempt to create string with null pointer"); + if (__beg == __end && __a == _Alloc()) return _S_empty_rep()._M_refcopy(); @@ -219,7 +223,8 @@ namespace std template basic_string<_CharT, _Traits, _Alloc>:: basic_string(const _CharT* __s, const _Alloc& __a) - : _M_dataplus(_S_construct(__s, __s + traits_type::length(__s), __a), __a) + : _M_dataplus(_S_construct(__s, __s ? __s + traits_type::length(__s) : 0, + __a), __a) { } template diff --git a/libstdc++-v3/testsuite/21_strings/ctor_copy_dtor.cc b/libstdc++-v3/testsuite/21_strings/ctor_copy_dtor.cc index 355ce4d5dcd..9247a9904b8 100644 --- a/libstdc++-v3/testsuite/21_strings/ctor_copy_dtor.cc +++ b/libstdc++-v3/testsuite/21_strings/ctor_copy_dtor.cc @@ -1,6 +1,6 @@ // 1999-06-04 bkoz -// Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc. +// Copyright (C) 1999, 2000, 2001, 2002 Free Software Foundation, Inc. // // 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 @@ -25,7 +25,7 @@ #include #include -int test01(void) +void test01(void) { bool test = true; typedef std::string::size_type csize_type; @@ -59,9 +59,6 @@ int test01(void) VERIFY( false ); } -#if 0 - // XXX These tests have been temporarily disabled. - //http://gcc.gnu.org/ml/libstdc++/2000-10/msg00033.html // basic_string(const char* s, size_type n, alloc) csz01 = str01.max_size(); // NB: As strlen(str_lit01) != csz01, this test is undefined. It @@ -105,7 +102,6 @@ int test01(void) catch(...) { VERIFY( false ); } -#endif // basic_string(const char* s, const allocator& a = allocator()) std::string str04(str_lit01); @@ -155,11 +151,6 @@ int test01(void) // basic_string(_InputIter begin, _InputIter end, const allocator& a) std::string str06(str01.begin(), str01.end()); VERIFY( str06 == str01 ); - -#ifdef DEBUG_ASSERT - assert(test); -#endif - return test; } void test02() @@ -171,9 +162,6 @@ void test02() // where _InputIter is integral [21.3.1 para 15] std::string s(10,0); VERIFY( s.size() == 10 ); -#ifdef DEBUG_ASSERT - assert(test); -#endif } void test03() @@ -189,9 +177,28 @@ void test03() std::string s2 (s1); VERIFY( s2.size() == 28 ); -#ifdef DEBUG_ASSERT - assert(test); -#endif + // Not defined, but libstdc++ throws an exception. + const char* bogus = 0; + try + { + std::string str1(bogus); + VERIFY( false ); + } + catch(std::exception& fail) + { + VERIFY( true ); + } + + // Not defined, but libstdc++ throws an exception. + try + { + std::string str2(bogus, 5); + VERIFY( false ); + } + catch(std::exception& fail) + { + VERIFY( true ); + } } int main() -- 2.30.2