--------------------- PatchSet 4613 Date: 2007/05/19 14:27:10 Author: amosjeffries Branch: ayjwork Tag: (none) Log: Add unit tests for [] access to strings. Fix old bug: append does not fully copy a string if \0 in source string Only occurs IFF dst buffer is large enough to accompany full output string. Members: src/SqString.cc:1.1.2.6->1.1.2.7 src/SqString.cci:1.1.2.1->1.1.2.2 src/tests/testString.cc:1.2.12.2->1.2.12.3 src/tests/testString.h:1.1.18.1->1.1.18.2 Index: squid3/src/SqString.cc =================================================================== RCS file: /cvsroot/squid-sf//squid3/src/SqString.cc,v retrieving revision 1.1.2.6 retrieving revision 1.1.2.7 diff -u -r1.1.2.6 -r1.1.2.7 --- squid3/src/SqString.cc 19 May 2007 06:28:41 -0000 1.1.2.6 +++ squid3/src/SqString.cc 19 May 2007 14:27:10 -0000 1.1.2.7 @@ -1,6 +1,6 @@ /* - * $Id: SqString.cc,v 1.1.2.6 2007/05/19 06:28:41 amosjeffries Exp $ + * $Id: SqString.cc,v 1.1.2.7 2007/05/19 14:27:10 amosjeffries Exp $ * * DEBUG: section 67 String * AUTHOR: Duane Wessels @@ -187,7 +187,8 @@ return; if (len_ + len < size_) { - strncat(buf_, str, len); + operator[](len_) = '\0'; + xmemcpy(buf_+len_, str, len); len_ += len; } else { unsigned int ssz = len_ + len; Index: squid3/src/SqString.cci =================================================================== RCS file: /cvsroot/squid-sf//squid3/src/SqString.cci,v retrieving revision 1.1.2.1 retrieving revision 1.1.2.2 diff -u -r1.1.2.1 -r1.1.2.2 --- squid3/src/SqString.cci 5 May 2007 16:09:00 -0000 1.1.2.1 +++ squid3/src/SqString.cci 19 May 2007 14:27:10 -0000 1.1.2.2 @@ -1,6 +1,6 @@ /* - * $Id: SqString.cci,v 1.1.2.1 2007/05/05 16:09:00 amosjeffries Exp $ + * $Id: SqString.cci,v 1.1.2.2 2007/05/19 14:27:10 amosjeffries Exp $ * * DEBUG: section 67 String * AUTHOR: Duane Wessels @@ -142,25 +142,22 @@ return strcmp(c_str(), aString.c_str()); } -/* FIXME: this is can perform buffer overflows and underflows! */ void SqString::set (char const *loc, char const ch) { - buf_[loc-buf_] = ch; + operator[](loc-buf_) = ch; } -/* FIXME: this is can perform buffer overflows and underflows! */ void SqString::cut (size_t newLength) { + operator[](newLength) = '\0'; len_ = newLength; - buf_[newLength] = '\0'; } -/* FIXME: this is can perform buffer overflows and underflows! */ void SqString::cutPointer (char const *loc) { + operator[](loc-buf_) = '\0'; len_ = loc-buf_; - buf_[len_] = '\0'; } Index: squid3/src/tests/testString.cc =================================================================== RCS file: /cvsroot/squid-sf//squid3/src/tests/testString.cc,v retrieving revision 1.2.12.2 retrieving revision 1.2.12.3 diff -u -r1.2.12.2 -r1.2.12.3 --- squid3/src/tests/testString.cc 6 May 2007 03:18:15 -0000 1.2.12.2 +++ squid3/src/tests/testString.cc 19 May 2007 14:27:10 -0000 1.2.12.3 @@ -83,7 +83,7 @@ void testString::testAppend() { - // FIXME: make tests for this. + // FIXME: make more tests for this. string aStr("hello"); aStr.append(" world"); @@ -123,7 +123,55 @@ cStr.append("rld\0 untroubled by things such as null termination", 10); CPPUNIT_ASSERT( !cStr.empty() ); CPPUNIT_ASSERT_EQUAL( 18, cStr.size() ); - CPPUNIT_ASSERT_EQUAL( (string)"hello world\0 untr", cStr ); + CPPUNIT_ASSERT( memcmp("hello world", cStr.c_str(), 11) == 0 ); + CPPUNIT_ASSERT( memcmp("hello world\0", cStr.c_str(), 12) == 0 ); + CPPUNIT_ASSERT( memcmp("hello world\0 untro", cStr.c_str(), 18) == 0 ); + CPPUNIT_ASSERT( memcmp("hello world\0 untro\0", cStr.c_str(), 19) == 0 ); +} + +void +testString::testAccess() +{ + string test; + test = "123456789a"; // to get a predictable length buffer. + + CPPUNIT_ASSERT_EQUAL( test.size(), 10 ); + +/* FIXME: flow checks do not seem to catch assert() sent from within code. */ + /* underflow handling test: _should_ fail with core dump. */ + /* this SHOULD be impossible due to unsigned type of parameter. */ +// CPPUNIT_ASSERT_ASSERTION_FAIL( CPPUNIT_ASSERT( test[-1] ) ); + + /* overflow handling test: _should_ fail with core dump. */ +// CPPUNIT_ASSERT_ASSERTION_FAIL( CPPUNIT_ASSERT( test[test.size()+10] ) ); + + /* [] access method (read and write) */ + CPPUNIT_ASSERT( test[0] == '1' ); + CPPUNIT_ASSERT( test[9] == 'a' ); + CPPUNIT_ASSERT( test[10] == '\0' ); + + test.append('T'); + CPPUNIT_ASSERT( test[10] == 'T' ); + CPPUNIT_ASSERT( test[11] == '\0' ); + CPPUNIT_ASSERT_EQUAL((string)"123456789aT", test); + + /* Random access inside buffer. */ + test[5] = 't'; + CPPUNIT_ASSERT( test[5] == 't' ); + CPPUNIT_ASSERT( test[11] == '\0' ); + CPPUNIT_ASSERT_EQUAL((string)"12345t789aT", test); + + /* border case at last position of string */ + test[9] = 'E'; + CPPUNIT_ASSERT( test[9] == 'E' ); + CPPUNIT_ASSERT( test[11] == '\0' ); + CPPUNIT_ASSERT_EQUAL((string)"12345t789ET", test); + + /* border case at EOS position */ + test[11] = 'F'; + CPPUNIT_ASSERT( test[11] == 'F' ); + CPPUNIT_ASSERT( test[12] == '\0' ); + CPPUNIT_ASSERT_EQUAL((string)"12345t789ETF", test); } void Index: squid3/src/tests/testString.h =================================================================== RCS file: /cvsroot/squid-sf//squid3/src/tests/testString.h,v retrieving revision 1.1.18.1 retrieving revision 1.1.18.2 diff -u -r1.1.18.1 -r1.1.18.2 --- squid3/src/tests/testString.h 6 May 2007 03:18:16 -0000 1.1.18.1 +++ squid3/src/tests/testString.h 19 May 2007 14:27:10 -0000 1.1.18.2 @@ -20,6 +20,7 @@ CPPUNIT_TEST( testBooleans ); CPPUNIT_TEST( testAppend ); CPPUNIT_TEST( testAssignments ); + CPPUNIT_TEST( testAccess ); CPPUNIT_TEST( testCstrMethods ); CPPUNIT_TEST( testSearch ); CPPUNIT_TEST_SUITE_END(); @@ -36,6 +37,7 @@ void testBooleans(); void testAppend(); void testAssignments(); + void testAccess(); void testCstrMethods(); void testSearch(); };