This is the mail archive of the
ecos-discuss@sources.redhat.com
mailing list for the eCos project.
string functions assertions
- To: ecos-discuss at sources dot redhat dot com
- Subject: [ECOS] string functions assertions
- From: Robin Farine <acnrf at dial dot eunet dot ch>
- Date: 09 Aug 2001 12:04:22 +0200
Hi,
The string functions defined in "packages/language/c/libc/string/<ver>/src" that
take a length argument such as memmove(...) fail when called with a length of 0
if compiled with CYGPKG_INFRA_DEBUG enabled. I suppose that it's legal to do
that so I added some length tests to those functions (these tests should get
optimized out when compiling with assertions disabled). The attached patch
contains the changes.
Robin
Index: memchr.cxx
===================================================================
RCS file: /usr/cvs/eCos/base/packages/language/c/libc/string/current/src/memchr.cxx,v
retrieving revision 1.1.1.1
diff -C4 -r1.1.1.1 memchr.cxx
*** memchr.cxx 8 Mar 2001 08:57:19 -0000 1.1.1.1
--- memchr.cxx 9 Aug 2001 09:31:03 -0000
***************
*** 68,76 ****
{
CYG_REPORT_FUNCNAMETYPE( "__memchr", "returning addr %08x" );
CYG_REPORT_FUNCARG3( "s=%08x, c=%d, n=%d", s, c, n );
! CYG_CHECK_DATA_PTR( s, "s is not a valid pointer!" );
#if defined(CYGIMP_LIBC_STRING_PREFER_SMALL_TO_FAST) || defined(__OPTIMIZE_SIZE__)
const unsigned char *src = (const unsigned char *) s;
--- 68,79 ----
{
CYG_REPORT_FUNCNAMETYPE( "__memchr", "returning addr %08x" );
CYG_REPORT_FUNCARG3( "s=%08x, c=%d, n=%d", s, c, n );
! if (n)
! {
! CYG_CHECK_DATA_PTR( s, "s is not a valid pointer!" );
! }
#if defined(CYGIMP_LIBC_STRING_PREFER_SMALL_TO_FAST) || defined(__OPTIMIZE_SIZE__)
const unsigned char *src = (const unsigned char *) s;
Index: memcmp.cxx
===================================================================
RCS file: /usr/cvs/eCos/base/packages/language/c/libc/string/current/src/memcmp.cxx,v
retrieving revision 1.1.1.1
diff -C4 -r1.1.1.1 memcmp.cxx
*** memcmp.cxx 8 Mar 2001 08:57:19 -0000 1.1.1.1
--- memcmp.cxx 9 Aug 2001 09:06:23 -0000
***************
*** 72,81 ****
CYG_REPORT_FUNCNAMETYPE( "__memcmp", "returning %d" );
CYG_REPORT_FUNCARG3( "s1=%08x, s2=%08x, n=%d", s1, s2, n );
! CYG_CHECK_DATA_PTR( s1, "s1 is not a valid pointer!" );
! CYG_CHECK_DATA_PTR( s2, "s2 is not a valid pointer!" );
#if defined(CYGIMP_LIBC_STRING_PREFER_SMALL_TO_FAST) || defined(__OPTIMIZE_SIZE__)
const unsigned char *m1 = (const unsigned char *) s1;
const unsigned char *m2 = (const unsigned char *) s2;
--- 72,84 ----
CYG_REPORT_FUNCNAMETYPE( "__memcmp", "returning %d" );
CYG_REPORT_FUNCARG3( "s1=%08x, s2=%08x, n=%d", s1, s2, n );
! if (n)
! {
! CYG_CHECK_DATA_PTR( s1, "s1 is not a valid pointer!" );
! CYG_CHECK_DATA_PTR( s2, "s2 is not a valid pointer!" );
! }
#if defined(CYGIMP_LIBC_STRING_PREFER_SMALL_TO_FAST) || defined(__OPTIMIZE_SIZE__)
const unsigned char *m1 = (const unsigned char *) s1;
const unsigned char *m2 = (const unsigned char *) s2;
Index: memmove.cxx
===================================================================
RCS file: /usr/cvs/eCos/base/packages/language/c/libc/string/current/src/memmove.cxx,v
retrieving revision 1.1.1.1
diff -C4 -r1.1.1.1 memmove.cxx
*** memmove.cxx 8 Mar 2001 08:57:19 -0000 1.1.1.1
--- memmove.cxx 9 Aug 2001 09:06:35 -0000
***************
*** 72,81 ****
CYG_REPORT_FUNCNAMETYPE( "__memmove", "returning %08x" );
CYG_REPORT_FUNCARG3( "s1=%08x, s2=%08x, n=%d", s1, s2, n );
! CYG_CHECK_DATA_PTR( s1, "s1 is not a valid pointer!" );
! CYG_CHECK_DATA_PTR( s2, "s2 is not a valid pointer!" );
if ((src < dst) && (dst < (src + n)))
{
// Have to copy backwards
--- 72,84 ----
CYG_REPORT_FUNCNAMETYPE( "__memmove", "returning %08x" );
CYG_REPORT_FUNCARG3( "s1=%08x, s2=%08x, n=%d", s1, s2, n );
! if (n)
! {
! CYG_CHECK_DATA_PTR( s1, "s1 is not a valid pointer!" );
! CYG_CHECK_DATA_PTR( s2, "s2 is not a valid pointer!" );
! }
if ((src < dst) && (dst < (src + n)))
{
// Have to copy backwards
Index: strncat.cxx
===================================================================
RCS file: /usr/cvs/eCos/base/packages/language/c/libc/string/current/src/strncat.cxx,v
retrieving revision 1.1.1.1
diff -C4 -r1.1.1.1 strncat.cxx
*** strncat.cxx 8 Mar 2001 08:57:19 -0000 1.1.1.1
--- strncat.cxx 9 Aug 2001 09:34:44 -0000
***************
*** 69,78 ****
{
CYG_REPORT_FUNCNAMETYPE( "__strncat", "returning %08x" );
CYG_REPORT_FUNCARG3( "s1=%08x, s2=%08x, n=%d", s1, s2, n );
! CYG_CHECK_DATA_PTR( s1, "s1 is not a valid pointer!" );
! CYG_CHECK_DATA_PTR( s2, "s2 is not a valid pointer!" );
#if defined(CYGIMP_LIBC_STRING_PREFER_SMALL_TO_FAST) || defined(__OPTIMIZE_SIZE__)
char *s = s1;
--- 69,81 ----
{
CYG_REPORT_FUNCNAMETYPE( "__strncat", "returning %08x" );
CYG_REPORT_FUNCARG3( "s1=%08x, s2=%08x, n=%d", s1, s2, n );
! if (n)
! {
! CYG_CHECK_DATA_PTR( s1, "s1 is not a valid pointer!" );
! CYG_CHECK_DATA_PTR( s2, "s2 is not a valid pointer!" );
! }
#if defined(CYGIMP_LIBC_STRING_PREFER_SMALL_TO_FAST) || defined(__OPTIMIZE_SIZE__)
char *s = s1;
Index: strncmp.cxx
===================================================================
RCS file: /usr/cvs/eCos/base/packages/language/c/libc/string/current/src/strncmp.cxx,v
retrieving revision 1.1.1.1
diff -C4 -r1.1.1.1 strncmp.cxx
*** strncmp.cxx 8 Mar 2001 08:57:19 -0000 1.1.1.1
--- strncmp.cxx 9 Aug 2001 09:46:55 -0000
***************
*** 70,85 ****
int retval;
CYG_REPORT_FUNCNAMETYPE( "__strncmp", "returning %d" );
CYG_REPORT_FUNCARG3( "s1=%08x, s2=%08x, n=%d", s1, s2, n );
CYG_CHECK_DATA_PTR( s1, "s1 is not a valid pointer!" );
CYG_CHECK_DATA_PTR( s2, "s2 is not a valid pointer!" );
#if defined(CYGIMP_LIBC_STRING_PREFER_SMALL_TO_FAST) || defined(__OPTIMIZE_SIZE__)
- if (n == 0)
- return 0;
-
while (n-- != 0 && *s1 == *s2)
{
if (n == 0 || *s1 == '\0' || *s2 == '\0')
break;
--- 70,88 ----
int retval;
CYG_REPORT_FUNCNAMETYPE( "__strncmp", "returning %d" );
CYG_REPORT_FUNCARG3( "s1=%08x, s2=%08x, n=%d", s1, s2, n );
+
+ if (n == 0)
+ {
+ CYG_REPORT_RETVAL( 0 );
+ return 0;
+ }
CYG_CHECK_DATA_PTR( s1, "s1 is not a valid pointer!" );
CYG_CHECK_DATA_PTR( s2, "s2 is not a valid pointer!" );
#if defined(CYGIMP_LIBC_STRING_PREFER_SMALL_TO_FAST) || defined(__OPTIMIZE_SIZE__)
while (n-- != 0 && *s1 == *s2)
{
if (n == 0 || *s1 == '\0' || *s2 == '\0')
break;
***************
*** 94,107 ****
return retval;
#else
const CYG_WORD *aligned_s1;
const CYG_WORD *aligned_s2;
-
- if (n == 0)
- {
- CYG_REPORT_RETVAL( 0 );
- return 0;
- } // if
// If s1 or s2 are unaligned, then compare bytes.
if (CYG_LIBC_STR_UNALIGNED2 (s1, s2))
{
--- 97,104 ----
Index: strncpy.cxx
===================================================================
RCS file: /usr/cvs/eCos/base/packages/language/c/libc/string/current/src/strncpy.cxx,v
retrieving revision 1.1.1.1
diff -C4 -r1.1.1.1 strncpy.cxx
*** strncpy.cxx 8 Mar 2001 08:57:19 -0000 1.1.1.1
--- strncpy.cxx 9 Aug 2001 09:35:35 -0000
***************
*** 69,78 ****
{
CYG_REPORT_FUNCNAMETYPE( "__strncpy", "returning %08x" );
CYG_REPORT_FUNCARG3( "s1=%08x, s2=%08x, n=%d", s1, s2, n );
! CYG_CHECK_DATA_PTR( s1, "s1 is not a valid pointer!" );
! CYG_CHECK_DATA_PTR( s2, "s2 is not a valid pointer!" );
#if defined(CYGIMP_LIBC_STRING_PREFER_SMALL_TO_FAST) || defined(__OPTIMIZE_SIZE__)
char *dscan;
const char *sscan;
--- 69,81 ----
{
CYG_REPORT_FUNCNAMETYPE( "__strncpy", "returning %08x" );
CYG_REPORT_FUNCARG3( "s1=%08x, s2=%08x, n=%d", s1, s2, n );
! if (n)
! {
! CYG_CHECK_DATA_PTR( s1, "s1 is not a valid pointer!" );
! CYG_CHECK_DATA_PTR( s2, "s2 is not a valid pointer!" );
! }
#if defined(CYGIMP_LIBC_STRING_PREFER_SMALL_TO_FAST) || defined(__OPTIMIZE_SIZE__)
char *dscan;
const char *sscan;
Index: strxfrm.cxx
===================================================================
RCS file: /usr/cvs/eCos/base/packages/language/c/libc/string/current/src/strxfrm.cxx,v
retrieving revision 1.1.1.1
diff -C4 -r1.1.1.1 strxfrm.cxx
*** strxfrm.cxx 8 Mar 2001 08:57:19 -0000 1.1.1.1
--- strxfrm.cxx 9 Aug 2001 09:42:24 -0000
***************
*** 71,80 ****
CYG_REPORT_FUNCNAMETYPE( "__strxfrm", "returning size %d" );
CYG_REPORT_FUNCARG3( "s1=%08x, s2=%08x, n=%d", s1, s2, n );
! if (s1 != NULL)
CYG_CHECK_DATA_PTR( s1, "s1 is not a valid pointer!" );
CYG_CHECK_DATA_PTR( s2, "s2 is not a valid pointer!" );
while (n-- > 0)
{
--- 71,82 ----
CYG_REPORT_FUNCNAMETYPE( "__strxfrm", "returning size %d" );
CYG_REPORT_FUNCARG3( "s1=%08x, s2=%08x, n=%d", s1, s2, n );
! if (n)
! {
CYG_CHECK_DATA_PTR( s1, "s1 is not a valid pointer!" );
+ }
CYG_CHECK_DATA_PTR( s2, "s2 is not a valid pointer!" );
while (n-- > 0)
{