diff --git a/libc/src/string/string_utils.h b/libc/src/string/string_utils.h --- a/libc/src/string/string_utils.h +++ b/libc/src/string/string_utils.h @@ -58,23 +58,26 @@ static inline char *string_token(char *__restrict src, const char *__restrict delimiter_string, char **__restrict saveptr) { + // Check for nullptr in src AND *saveptr first + if (src == nullptr && ((src = *saveptr) == nullptr)) + return nullptr; + cpp::Bitset<256> delimiter_set; - for (; *delimiter_string; ++delimiter_string) + for (; *delimiter_string != '\0'; ++delimiter_string) delimiter_set.set(*delimiter_string); - src = src ? src : *saveptr; - for (; *src && delimiter_set.test(*src); ++src) + for (; *src != '\0' && delimiter_set.test(*src); ++src) ; - if (!*src) { + if (*src == '\0') { *saveptr = src; return nullptr; } char *token = src; - for (; *src && !delimiter_set.test(*src); ++src) - ; - if (*src) { - *src = '\0'; - ++src; + for (; *src != '\0'; ++src) { + if (delimiter_set.test(*src)) { + *src++ = '\0'; + break; + } } *saveptr = src; return token;