OSDN Git Service

2002-03-12 Benjamin Kosnik <bkoz@redhat.com>
authorbkoz <bkoz@138bc75d-0d04-0410-961f-82ee72b054a4>
Tue, 12 Mar 2002 22:10:34 +0000 (22:10 +0000)
committerbkoz <bkoz@138bc75d-0d04-0410-961f-82ee72b054a4>
Tue, 12 Mar 2002 22:10:34 +0000 (22:10 +0000)
* 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.

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

libstdc++-v3/ChangeLog
libstdc++-v3/include/bits/basic_string.tcc
libstdc++-v3/testsuite/21_strings/ctor_copy_dtor.cc

index 078d795..9e07ffd 100644 (file)
@@ -1,3 +1,12 @@
+2002-03-12  Benjamin Kosnik  <bkoz@redhat.com>
+
+       * 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  <bkoz@redhat.com>
 
        * acinclude.m4 (GLIBCPP_CHECK_WCHAR_T_SUPPORT): Check for wctype.h.
index 01a3fe6..dc6db6e 100644 (file)
@@ -139,6 +139,10 @@ namespace std
       {
        size_type __dnew = static_cast<size_type>(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<typename _CharT, typename _Traits, typename _Alloc>
     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<typename _CharT, typename _Traits, typename _Alloc>
index 355ce4d..9247a99 100644 (file)
@@ -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 <stdexcept>
 #include <testsuite_hooks.h>
 
-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()