aboutsummaryrefslogtreecommitdiff
path: root/lib/str.c
diff options
context:
space:
mode:
authorJoseph Hunkeler <jhunkeler@gmail.com>2020-03-26 02:15:59 -0400
committerJoseph Hunkeler <jhunkeler@gmail.com>2020-03-26 02:15:59 -0400
commit37ec63ece45c7f27f576e1fb5911b55630125d03 (patch)
tree843d82c165c784997bedf2ad36f078f932b1643d /lib/str.c
parentd0925dcc2e34d6f9a8c554ca97c4d75784e6f69d (diff)
downloadspmc-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.c11
1 files changed, 6 insertions, 5 deletions
diff --git a/lib/str.c b/lib/str.c
index 3736e2f..0e2f90e 100644
--- a/lib/str.c
+++ b/lib/str.c
@@ -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;