From: Wilfried Göesgens Date: Thu, 1 Oct 2009 21:54:18 +0000 (+0000) Subject: * be a bit more picky about string ends in StrBufExtract_token X-Git-Tag: v7.86~815 X-Git-Url: https://code.citadel.org/?a=commitdiff_plain;h=63bd165286b1c223c5c1f05823cdbb2150a64e49;p=citadel.git * be a bit more picky about string ends in StrBufExtract_token * add first test for StrBufExtract_token --- diff --git a/libcitadel/lib/stringbuf.c b/libcitadel/lib/stringbuf.c index d348fac64..00d935896 100644 --- a/libcitadel/lib/stringbuf.c +++ b/libcitadel/lib/stringbuf.c @@ -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) diff --git a/libcitadel/tests/stringbuf_test.c b/libcitadel/tests/stringbuf_test.c index cfcd932b1..772cd9a70 100644 --- a/libcitadel/tests/stringbuf_test.c +++ b/libcitadel/tests/stringbuf_test.c @@ -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); }