From 084892a9d3c7e641e31f0c565928104b28a7898c Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Sun, 24 Mar 2024 19:12:11 -0400 Subject: Fix condition that ruined sptr. * Pass temporary pointer to strsep --- src/str.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'src/str.c') diff --git a/src/str.c b/src/str.c index a6a848c..4e970b7 100644 --- a/src/str.c +++ b/src/str.c @@ -197,11 +197,11 @@ char** split(char *_sptr, const char* delim, size_t max) if (max && i > max) { break; } - split_alloc += num_chars(sptr, delim[i]); + split_alloc = num_chars(sptr, delim[i]); } // Preallocate enough records based on the number of delimiters - char **result = (char **)calloc(split_alloc + 2, sizeof(char *)); + char **result = calloc(split_alloc + 2, sizeof(result[0])); if (!result) { guard_free(sptr); return NULL; @@ -210,7 +210,6 @@ char** split(char *_sptr, const char* delim, size_t max) // No delimiter, but the string was not NULL, so return the original string if (split_alloc == 0) { result[0] = sptr; - result[1] = NULL; return result; } @@ -218,7 +217,8 @@ char** split(char *_sptr, const char* delim, size_t max) int i = 0; size_t x = max; char *token = NULL; - while((token = strsep(&sptr, delim)) != NULL) { + char *sptr_tmp = sptr; + while((token = strsep(&sptr_tmp, delim)) != NULL) { result[i] = calloc(OMC_BUFSIZ, sizeof(char)); if (x < max) { strcat(result[i], strstr(orig, delim) + 1); @@ -230,10 +230,12 @@ char** split(char *_sptr, const char* delim, size_t max) strcpy(result[i], token); } i++; - --x; + if (x > 0) { + --x; + } //memcpy(result[i], token, strlen(token) + 1); // copy the string contents into the record } - sptr = NULL; + guard_free(sptr); return result; } -- cgit