]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
Fix 1-off bug left after strnlen bug removal.
authoramosjeffries <>
Sun, 20 May 2007 14:29:44 +0000 (14:29 +0000)
committeramosjeffries <>
Sun, 20 May 2007 14:29:44 +0000 (14:29 +0000)
Also:
 * Harden memory allocations to SqString.
 * Added more unit tests for strings.

src/SqString.cc
src/SqString.h
src/tests/testString.cc
src/tests/testString.h

index eb0e6a5d4b9a36bbe3805cee579b52235d270539..32b4d8ec57bde4a60f51bcc4bfcfd8200ad2a5fc 100644 (file)
@@ -1,6 +1,6 @@
 
 /*
- * $Id: SqString.cc,v 1.5 2007/05/20 04:22:06 amosjeffries Exp $
+ * $Id: SqString.cc,v 1.6 2007/05/20 08:29:44 amosjeffries Exp $
  *
  * DEBUG: section 67    String
  * AUTHOR: Duane Wessels
 void
 SqString::initBuf(size_t sz)
 {
+    size_t bsz;
     PROF_start(StringInitBuf);
     clear();
-    buf_ = (char *)memAllocString(sz, &sz);
     assert(sz < 65536);
-    size_ = sz;
+    buf_ = (char *)memAllocString(sz, &bsz);
+    assert(bsz < 65536);
+    assert(bsz >= sz);
+    size_ = bsz;
     PROF_stop(StringInitBuf);
 }
 
 void
-SqString::limitInit(const char *str, int len)
+SqString::limitInit(const char *str, unsigned int len)
 {
     PROF_start(StringLimitInit);
     assert(this && str);
@@ -185,15 +188,16 @@ SqString::append(const char *str, int len)
     if(len < 1 || str == NULL)
         return;
 
-    if (len_ + len < size_) {
+    if ( (len_ + len +1) < size_) {
         operator[](len_+len) = '\0';
         xmemcpy(buf_+len_, str, len);
         len_ += len;
     } else {
         unsigned int ssz = len_ + len;
         unsigned int bsz = len_ + len + 1;
-        char* tmp = (char *)memAllocString(ssz, &bsz);
+        char* tmp = (char *)memAllocString(bsz, &bsz);
         assert(bsz < 65536);
+        assert(bsz > ssz);
 
         if (buf_)
             xmemcpy(tmp, buf_, len_);
@@ -201,7 +205,7 @@ SqString::append(const char *str, int len)
         if (len)
             xmemcpy(tmp + len_, str, len);
 
-        tmp[ssz + 1] = '\0';
+        tmp[ssz] = '\0';
 
         clear();
 
@@ -232,7 +236,7 @@ SqString::append (char chr)
 void
 SqString::append(SqString const &old)
 {
-    append (old.c_str(), old.len_);
+    append (old.c_str(), old.size());
 }
 
 const char&
index 8362b0c117eb481de7dd046ac2e766ddf597b55d..c330bb44c01813403bc8e17bbdfafc37e1849c59 100644 (file)
@@ -1,6 +1,6 @@
 
 /*
- * $Id: SqString.h,v 1.2 2007/05/18 16:56:18 amosjeffries Exp $
+ * $Id: SqString.h,v 1.3 2007/05/20 08:29:44 amosjeffries Exp $
  *
  * DEBUG: section 67    String
  * AUTHOR: Duane Wessels
@@ -135,7 +135,7 @@ public:
 
 #endif
 
-    void limitInit(const char *str, int len);
+    void limitInit(const char *str, unsigned int len);
 private:
     void initBuf(size_t sz);
     void init (char const *);
index 083693ae36167bbb86c82e25d51d917f268be301..58baa4f9026df3495c8c4fd141c5df58c89d7915 100644 (file)
@@ -123,7 +123,55 @@ testString::testAppend()
     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 69cdd42b091be8d91d4190002f7ea87ea1d8b065..52951a2fefc9b9f1f451d0c69d376eca0da92e85 100644 (file)
@@ -20,6 +20,7 @@ class testString : public CPPUNIT_NS::TestFixture
     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 @@ protected:
     void testBooleans();
     void testAppend();
     void testAssignments();
+    void testAccess();
     void testCstrMethods();
     void testSearch();
 };