From 000265bfbf181c41bd932e4bf48e187df2496cb6 Mon Sep 17 00:00:00 2001 From: Iku Date: Wed, 8 Feb 2017 12:14:35 +0100 Subject: Remove problematic split() function Major issues with split(): 1. It allocates too many array entries 2. It will seek into out of bounds memory 3. It seeds to many nulls See https://github.com/andmarti1424/sc-im/issues/116 for details. This commit removes split() and replaces its use with some stdlib functions. Tests performed: - ./scim --half_page_scroll (verify using :set) - ./scim --half_page_scroll=2 (verify using :set) - !ls -l --- src/utils/string.c | 27 --------------------------- src/utils/string.h | 1 - 2 files changed, 28 deletions(-) (limited to 'src/utils') diff --git a/src/utils/string.c b/src/utils/string.c index 753f37f..524637a 100755 --- a/src/utils/string.c +++ b/src/utils/string.c @@ -175,33 +175,6 @@ int is_idchar (int d) { return 0; } -char ** split(char *string, const char delimiter, int lastnull) { - int length = 0, count = 0, i = 0, j = 0; - while(*(string++)) { - if (*string == delimiter) count++; - length++; - } - string -= (length + 1); // string was incremented one more than length - char **array = (char **)malloc(sizeof(char *) * (length + 1 + lastnull)); - char ** base = array; - for(i = 0; i < (count + 1); i++) { - j = 0; - while(string[j] != delimiter) j++; - j++; - * array = (char *) malloc(sizeof(char) * j); - memcpy(*array, string, (j-1)); - (*array)[j-1] = '\0'; - string += j; - array++; - } - if (lastnull) { - *array = NULL; - array++; - } - *array = '\0'; - return base; -} - char * rtrim(char * string, char junk) { char * original = string + strlen(string); while(*--original == junk); diff --git a/src/utils/string.h b/src/utils/string.h index be54d9d..4dab3f9 100755 --- a/src/utils/string.h +++ b/src/utils/string.h @@ -16,7 +16,6 @@ void subst(char * s, char from, char to); int is_idchar (int d); int str_in_str(char * s, char * b); int wstr_in_wstr(wchar_t * s, wchar_t * b); -char ** split(char *string, const char delimiter, int lastnull); char * ltrim(char *string, char junk); char * rtrim(char * string, char junk); int isnumeric(char * string); -- cgit v1.2.3