Fix bug in fake g_strsplit

Lack of NULL termination caused an occasional crash
This commit is contained in:
Evan Miller 2020-08-10 16:58:11 -04:00
parent cc292b3f0d
commit 4ca16b8edf

View File

@ -25,14 +25,14 @@ int g_str_equal(const void *str1, const void *str2) {
char **g_strsplit(const char *haystack, const char *needle, int something) { char **g_strsplit(const char *haystack, const char *needle, int something) {
char **ret = NULL; char **ret = NULL;
char *found = NULL; char *found = NULL;
size_t components = 1; size_t components = 2; // last component + terminating NULL
while ((found = strstr(haystack, needle))) { while ((found = strstr(haystack, needle))) {
components++; components++;
haystack = found + strlen(needle); haystack = found + strlen(needle);
} }
ret = malloc(components * sizeof(char *)); ret = calloc(components, sizeof(char *));
int i = 0; int i = 0;
while ((found = strstr(haystack, needle))) { while ((found = strstr(haystack, needle))) {