This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
[v3] char_traits error checking
- From: Benjamin Kosnik <bkoz at redhat dot com>
- To: libstdc++ at gcc dot gnu dot org
- Date: Mon, 11 Mar 2002 16:45:18 -0800
- Subject: [v3] char_traits error checking
I first noticed this when looking at the numeric_limits problems
posted late last week:
#include <string>
int main()
{
std::string(0);
return 0;
}
This cores, at least on x86/linux. I'd think this is something that
should be avoided, if possible.
Here's one way to solve it. Thoughts?
tested x86/linux
2002-03-11 Benjamin Kosnik <bkoz@redhat.com>
* include/bits/char_traits.h (char_traits<char>::length): Fix for
null case.
(char_traits<wchar_t>::length): Same.
(char_traits<char>::find): Same.
(char_traits<wchar_t>::find): Same.
(char_traits<char>::compare): Same.
(char_traits<wchar_t>::compare): Same.
* testsuite/21_strings/char_traits_requirements.cc (test02): Add
tests for null strings.
(test01): Same.
Index: include/bits/char_traits.h
===================================================================
RCS file: /cvs/gcc/gcc/libstdc++-v3/include/bits/char_traits.h,v
retrieving revision 1.12
diff -c -p -r1.12 char_traits.h
*** char_traits.h 2002/01/04 21:27:31 1.12
--- char_traits.h 2002/03/12 00:40:00
***************
*** 1,6 ****
// Character Traits for use by standard string and iostream -*- C++ -*-
! // Copyright (C) 1997, 1998, 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,7 ----
// Character Traits for use by standard string and iostream -*- C++ -*-
! // Copyright (C) 1997, 1998, 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
*************** namespace std
*** 84,90 ****
length(const char_type* __s)
{
const char_type* __p = __s;
! while (*__p) ++__p;
return (__p - __s);
}
--- 85,92 ----
length(const char_type* __s)
{
const char_type* __p = __s;
! while (__p && *__p)
! ++__p;
return (__p - __s);
}
*************** namespace std
*** 156,170 ****
static int
compare(const char_type* __s1, const char_type* __s2, size_t __n)
! { return memcmp(__s1, __s2, __n); }
static size_t
length(const char_type* __s)
! { return strlen(__s); }
static const char_type*
find(const char_type* __s, size_t __n, const char_type& __a)
! { return static_cast<const char_type*>(memchr(__s, __a, __n)); }
static char_type*
move(char_type* __s1, const char_type* __s2, size_t __n)
--- 158,174 ----
static int
compare(const char_type* __s1, const char_type* __s2, size_t __n)
! { return __s1 ? memcmp(__s1, __s2, __n) : __s1 - __s2; }
static size_t
length(const char_type* __s)
! { return (__s ? strlen(__s) : 0); }
static const char_type*
find(const char_type* __s, size_t __n, const char_type& __a)
! {
! return static_cast<const char_type*>(__s ? memchr(__s, __a, __n) : 0);
! }
static char_type*
move(char_type* __s1, const char_type* __s2, size_t __n)
*************** namespace std
*** 225,239 ****
static int
compare(const char_type* __s1, const char_type* __s2, size_t __n)
! { return wmemcmp(__s1, __s2, __n); }
static size_t
length(const char_type* __s)
! { return wcslen(__s); }
static const char_type*
find(const char_type* __s, size_t __n, const char_type& __a)
! { return wmemchr(__s, __a, __n); }
static char_type*
move(char_type* __s1, const char_type* __s2, int_type __n)
--- 229,243 ----
static int
compare(const char_type* __s1, const char_type* __s2, size_t __n)
! { return __s1 ? wmemcmp(__s1, __s2, __n) : __s1 - __s2; }
static size_t
length(const char_type* __s)
! { return (__s ? wcslen(__s) : 0); }
static const char_type*
find(const char_type* __s, size_t __n, const char_type& __a)
! { return (__s ? wmemchr(__s, __a, __n) : 0); }
static char_type*
move(char_type* __s1, const char_type* __s2, int_type __n)
Index: testsuite/21_strings/char_traits_requirements.cc
===================================================================
RCS file: /cvs/gcc/gcc/libstdc++-v3/testsuite/21_strings/char_traits_requirements.cc,v
retrieving revision 1.3
diff -c -p -r1.3 char_traits_requirements.cc
*** char_traits_requirements.cc 2001/08/07 03:38:28 1.3
--- char_traits_requirements.cc 2002/03/12 00:40:00
***************
*** 1,6 ****
// 1999-06-03 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-03 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
***************
*** 23,29 ****
#include <string>
#include <testsuite_hooks.h>
! int test01(void)
{
bool test = true;
const std::string str_01("zuma beach");
--- 23,29 ----
#include <string>
#include <testsuite_hooks.h>
! void test01(void)
{
bool test = true;
const std::string str_01("zuma beach");
*************** int test01(void)
*** 101,114 ****
c2 = array1[0];
VERIFY( c1 != c2 );
! #ifdef DEBUG_ASSERT
! assert(test);
! #endif
! return test;
}
#if _GLIBCPP_USE_WCHAR_T
! int test02(void)
{
bool test = true;
const std::wstring str_01(L"zuma beach");
--- 101,114 ----
c2 = array1[0];
VERIFY( c1 != c2 );
! // Tests for NULL-strings.
! std::char_traits<char>::length(0);
! std::char_traits<char>::find(0, 4, 'n');
! std::char_traits<char>::compare(0, 0, 4);
}
#if _GLIBCPP_USE_WCHAR_T
! void test02(void)
{
bool test = true;
const std::wstring str_01(L"zuma beach");
*************** int test02(void)
*** 185,196 ****
c1 = *(str_01.data());
c2 = array1[0];
VERIFY( c1 != c2 );
-
- #ifdef DEBUG_ASSERT
- assert(test);
- #endif
! return test;
}
#endif //_GLIBCPP_USE_WCHAR_T
--- 185,195 ----
c1 = *(str_01.data());
c2 = array1[0];
VERIFY( c1 != c2 );
! // Tests for NULL-strings.
! std::char_traits<wchar_t>::length(0);
! std::char_traits<wchar_t>::find(0, 4, 'n');
! std::char_traits<wchar_t>::compare(0, 0, 4);
}
#endif //_GLIBCPP_USE_WCHAR_T
*************** int main()
*** 202,204 ****
--- 201,207 ----
#endif
return 0;
}