diff options
| author | Joseph Hunkeler <jhunkeler@gmail.com> | 2024-03-24 19:12:11 -0400 | 
|---|---|---|
| committer | Joseph Hunkeler <jhunkeler@gmail.com> | 2024-03-24 19:12:11 -0400 | 
| commit | 084892a9d3c7e641e31f0c565928104b28a7898c (patch) | |
| tree | 3b10f72eab48c9c2074d00a023acc8de43c1e8d9 /src | |
| parent | 262ce83531a784fd3c6d71b62861d0f5ac5cb1d7 (diff) | |
| download | stasis-084892a9d3c7e641e31f0c565928104b28a7898c.tar.gz | |
Fix condition that ruined sptr.
* Pass temporary pointer to strsep
Diffstat (limited to 'src')
| -rw-r--r-- | src/str.c | 14 | 
1 files changed, 8 insertions, 6 deletions
| @@ -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;  } | 
