diff options
author | Joseph Hunkeler <jhunkeler@gmail.com> | 2020-03-26 02:15:59 -0400 |
---|---|---|
committer | Joseph Hunkeler <jhunkeler@gmail.com> | 2020-03-26 02:15:59 -0400 |
commit | 37ec63ece45c7f27f576e1fb5911b55630125d03 (patch) | |
tree | 843d82c165c784997bedf2ad36f078f932b1643d /lib/str.c | |
parent | d0925dcc2e34d6f9a8c554ca97c4d75784e6f69d (diff) | |
download | spmc-37ec63ece45c7f27f576e1fb5911b55630125d03.tar.gz |
Fix segfault caused by freeing records for no reason:
* split() returns the input string when no delimiters are found
Diffstat (limited to 'lib/str.c')
-rw-r--r-- | lib/str.c | 11 |
1 files changed, 6 insertions, 5 deletions
@@ -196,11 +196,6 @@ char** split(char *_sptr, const char* delim) split_alloc += num_chars(sptr, delim[i]); } - if (split_alloc == 0) { - free(orig); - return NULL; - } - // Preallocate enough records based on the number of delimiters char **result = (char **)calloc(split_alloc + 2, sizeof(char *)); if (!result) { @@ -208,6 +203,12 @@ char** split(char *_sptr, const char* delim) return NULL; } + // No delimiter, but the string was not NULL, so return the original string + if (split_alloc == 0) { + result[0] = sptr; + return result; + } + // Separate the string into individual parts and store them in the result array int i = 0; char *token = NULL; |