From 37ec63ece45c7f27f576e1fb5911b55630125d03 Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Thu, 26 Mar 2020 02:15:59 -0400 Subject: Fix segfault caused by freeing records for no reason: * split() returns the input string when no delimiters are found --- lib/str.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'lib/str.c') 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; -- cgit