aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoseph Hunkeler <jhunkeler@gmail.com>2024-03-24 19:12:11 -0400
committerJoseph Hunkeler <jhunkeler@gmail.com>2024-03-24 19:12:11 -0400
commit084892a9d3c7e641e31f0c565928104b28a7898c (patch)
tree3b10f72eab48c9c2074d00a023acc8de43c1e8d9
parent262ce83531a784fd3c6d71b62861d0f5ac5cb1d7 (diff)
downloadstasis-084892a9d3c7e641e31f0c565928104b28a7898c.tar.gz
Fix condition that ruined sptr.
* Pass temporary pointer to strsep
-rw-r--r--src/str.c14
1 files changed, 8 insertions, 6 deletions
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;
}