* be a bit more picky about string ends in StrBufExtract_token
authorWilfried Göesgens <willi@citadel.org>
Thu, 1 Oct 2009 21:54:18 +0000 (21:54 +0000)
committerWilfried Göesgens <willi@citadel.org>
Thu, 1 Oct 2009 21:54:18 +0000 (21:54 +0000)
* add first test for StrBufExtract_token

libcitadel/lib/stringbuf.c
libcitadel/tests/stringbuf_test.c

index d348fac6491b985f67b532331f7cc2e9baa3f0e6..00d935896c561591766c769e41b14ccb81122922 100644 (file)
@@ -1183,12 +1183,15 @@ int StrBufNum_tokens(const StrBuf *source, char tok)
 int StrBufRemove_token(StrBuf *Source, int parmnum, char separator)
 {
        int ReducedBy;
-       char *d, *s;            /* dest, source */
+       char *d, *s, *end;              /* dest, source */
        int count = 0;
 
        /* Find desired @parameter */
+       end = Source->buf + Source->BufUsed;
        d = Source->buf;
-       while (count < parmnum) {
+       while ((count < parmnum) &&
+              (d <= end))
+       {
                /* End of string, bail! */
                if (!*d) {
                        d = NULL;
@@ -1199,11 +1202,14 @@ int StrBufRemove_token(StrBuf *Source, int parmnum, char separator)
                }
                d++;
        }
-       if (!d) return 0;               /* @Parameter not found */
+       if ((d == NULL) || (d >= end))
+               return 0;               /* @Parameter not found */
 
        /* Find next @parameter */
        s = d;
-       while (*s && *s != separator) {
+       while ((*s && *s != separator) &&
+              (s <= end))
+       {
                s++;
        }
        if (*s == separator)
index cfcd932b1efd65ed5e2b23ad6ebf1a55810a60e6..772cd9a704bbf29242ead5feaba423f3fbed0846 100644 (file)
@@ -290,6 +290,13 @@ static void TestNextLine_LongLine(void)
 }
 
 
+static void TestStrBufRemove_token_NotThere(void)
+{
+       StrBuf *Test = NewStrBufPlain(HKEY(" 127.0.0.1"));
+       StrBufRemove_token(Test, 0, ',');
+       TestRevalidateStrBuf(Test);
+       FreeStrBuf(&Test);
+}
 
 /*
 Some samples from the original...
@@ -322,6 +329,9 @@ Some samples from the original...
 */
 
 
+
+
+
 static void AddStrBufSimlpeTests(void)
 {
        CU_pSuite pGroup = NULL;
@@ -347,6 +357,8 @@ static void AddStrBufSimlpeTests(void)
        pTest = CU_add_test(pGroup, "TestNextLine_twolines", TestNextLine_twolines);
        pTest = CU_add_test(pGroup, "TestNextLine_LongLine", TestNextLine_LongLine);
        
+       pGroup = CU_add_suite("TestStrBufRemove_token", NULL, NULL);
+       pTest = CU_add_test(pGroup, "TestStrBufRemove_token_NotThere", TestStrBufRemove_token_NotThere);
 
 }