Fix tor_strtok_r_impl and test cases per bug #5091

==

Nick here. I tweaked this patch a little to make it apply cleanly to
master, to extract some common code into a function, and to replace
snprintf with tor_snprintf.

-- nickm
This commit is contained in:
nils
2012-03-30 11:01:21 -04:00
committed by Nick Mathewson
parent 56e0959d2a
commit efb8a09f41
3 changed files with 77 additions and 33 deletions
+27 -9
View File
@@ -546,25 +546,43 @@ const char TOR_TOLOWER_TABLE[256] = {
240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255,
};
/** Helper for tor_strtok_r_impl: Advances cp past all characters in
* <b>sep</b>, and returns its new value. */
static char *
strtok_helper(char *cp, const char *sep)
{
if (sep[1]) {
while (*cp && strchr(sep, *cp))
++cp;
} else {
while (*cp && *cp == *sep)
++cp;
}
return cp;
}
/** Implementation of strtok_r for platforms whose coders haven't figured out
* how to write one. Hey guys! You can use this code here for free! */
char *
tor_strtok_r_impl(char *str, const char *sep, char **lasts)
{
char *cp, *start;
if (str)
start = cp = *lasts = str;
else if (!*lasts)
return NULL;
else
start = cp = *lasts;
tor_assert(*sep);
if (str) {
str = strtok_helper(str, sep);
if (!*str)
return NULL;
start = cp = *lasts = str;
} else if (!*lasts || !**lasts) {
return NULL;
} else {
start = cp = *lasts;
}
if (sep[1]) {
while (*cp && !strchr(sep, *cp))
++cp;
} else {
tor_assert(strlen(sep) == 1);
cp = strchr(cp, *sep);
}
@@ -572,7 +590,7 @@ tor_strtok_r_impl(char *str, const char *sep, char **lasts)
*lasts = NULL;
} else {
*cp++ = '\0';
*lasts = cp;
*lasts = strtok_helper(cp, sep);
}
return start;
}