This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [v3] char_traits error checking



> True, of course.  I was hoping to make this as fast as possible, given
> that this is an extremely common code path.

Always nice.

Here's another way to do this, with testcase. This way throws an exception.

To be honest, I don't really care what goes in, but I think the testsuite 
addition should definitely be checked in.

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.

Index: include/bits/basic_string.tcc
===================================================================
RCS file: /cvs/gcc/gcc/libstdc++-v3/include/bits/basic_string.tcc,v
retrieving revision 1.21
diff -c -p -r1.21 basic_string.tcc
*** basic_string.tcc	2002/03/08 06:05:18	1.21
--- basic_string.tcc	2002/03/12 18:58:05
*************** namespace std
*** 139,144 ****
--- 139,148 ----
        {
  	size_type __dnew = static_cast<size_type>(distance(__beg, __end));
  
+ 	// NB: Not required, but considered best practice.
+ 	if (__beg == _InIter(0))
+ 	  __throw_logic_error("attempt to create string with null value");
+ 
  	if (__beg == __end && __a == _Alloc())
  	  return _S_empty_rep()._M_refcopy();
  
*************** namespace std
*** 219,225 ****
    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)
      { }
  
    template<typename _CharT, typename _Traits, typename _Alloc>
--- 223,230 ----
    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 ? __s + 
traits_type::length(__s) : 0, 
! 			       __a), __a)
      { }
  
    template<typename _CharT, typename _Traits, typename _Alloc>
Index: testsuite/21_strings/ctor_copy_dtor.cc
===================================================================
RCS file: /cvs/gcc/gcc/libstdc++-v3/testsuite/21_strings/ctor_copy_dtor.cc,v
retrieving revision 1.8
diff -c -p -r1.8 ctor_copy_dtor.cc
*** ctor_copy_dtor.cc	2001/08/07 03:38:28	1.8
--- ctor_copy_dtor.cc	2002/03/12 18:58:07
***************
*** 1,6 ****
  // 1999-06-04 bkoz
  
! // Copyright (C) 1999, 2000, 2001 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
--- 1,6 ----
  // 1999-06-04 bkoz
  
! // 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,31 ****
  #include <stdexcept>
  #include <testsuite_hooks.h>
  
! int test01(void)
  {
    bool test = true;
    typedef std::string::size_type csize_type;
--- 25,31 ----
  #include <stdexcept>
  #include <testsuite_hooks.h>
  
! void test01(void)
  {
    bool test = true;
    typedef std::string::size_type csize_type;
*************** int test01(void)
*** 59,67 ****
      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
--- 59,64 ----
*************** int test01(void)
*** 105,111 ****
    catch(...) {
      VERIFY( false );
    }
- #endif
  
    // basic_string(const char* s, const allocator& a = allocator())
    std::string str04(str_lit01);
--- 102,107 ----
*************** int test01(void)
*** 155,165 ****
    //   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()
--- 151,156 ----
*************** void test02()
*** 171,179 ****
    // 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()
--- 162,167 ----
*************** void test03()
*** 189,197 ****
    std::string s2 (s1);
    VERIFY( s2.size() == 28 );
  
! #ifdef DEBUG_ASSERT
!   assert(test);
! #endif
  }
  
  int main()
--- 177,204 ----
    std::string s2 (s1);
    VERIFY( s2.size() == 28 );
  
!   // 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()


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]