From 2719ec4d6d38ea8eac1380fb9b530c5cad341d2c Mon Sep 17 00:00:00 2001 From: Rot127 Date: Sat, 20 Apr 2024 03:17:25 -0500 Subject: [PATCH] Revert "Fix leaks in cstest." This reverts commit bf8ee125b0c58f9c794eb081a69c80f8a71825cd. --- suite/cstest/include/helper.h | 2 +- suite/cstest/src/capstone_test.c | 3 +- suite/cstest/src/helper.c | 60 +++++++++++++------------------- 3 files changed, 27 insertions(+), 38 deletions(-) diff --git a/suite/cstest/include/helper.h b/suite/cstest/include/helper.h index 1fceefe90b..fcdeabffaa 100644 --- a/suite/cstest/include/helper.h +++ b/suite/cstest/include/helper.h @@ -17,7 +17,7 @@ #define X86_32 1 #define X86_64 2 -char **split(char *str, const char *delim, int *size); +char **split(char *str, char *delim, int *size); void print_strs(char **list_str, int size); void free_strs(char **list_str, int size); void add_str(char **src, const char *format, ...); diff --git a/suite/cstest/src/capstone_test.c b/suite/cstest/src/capstone_test.c index 10aa0baa66..cbc72ed010 100644 --- a/suite/cstest/src/capstone_test.c +++ b/suite/cstest/src/capstone_test.c @@ -243,8 +243,6 @@ void test_single_issue(csh *handle, cs_mode mode, char *line, int detail) } count = cs_disasm(*handle, code, size_byte, offset, 0, &insn); - free_strs(list_byte, size_byte); - free(code); for (i = 0; i < count; ++i) { tmp = (char *)malloc(strlen(insn[i].mnemonic) + strlen(insn[i].op_str) + 100); strcpy(tmp, insn[i].mnemonic); @@ -297,6 +295,7 @@ void test_single_issue(csh *handle, cs_mode mode, char *line, int detail) cs_free(insn, count); free_strs(list_part, size_part); + free_strs(list_byte, size_byte); free(cs_result); // free_strs(list_part_cs_result, size_part_cs_result); free_strs(list_part_issue_result, size_part_issue_result); diff --git a/suite/cstest/src/helper.c b/suite/cstest/src/helper.c index 9a5e9bce7c..ccf9f1d466 100644 --- a/suite/cstest/src/helper.c +++ b/suite/cstest/src/helper.c @@ -3,44 +3,34 @@ #include "helper.h" -#include - -/// Splits a string into a list of strings, separated by the given delimeter. -/// The last element in the list is always a duplicate of @str. -/// @str is not freed. -/// The number of elements in the list is written to @size -char **split(char *str, const char *delim, int *size) { - // Count the number of delimeters in the string - uint32_t elem_cnt = 0; - uint32_t delim_len = strlen(delim); - char *split_at = NULL; - char *str_iter = str; - // Count delimeters - while ((split_at = strstr(str_iter, delim)) != NULL) { - str_iter = split_at + delim_len; - if (split_at != str && split_at == str + strlen(str) - delim_len) { - // Don't increment if the delimeter is at the very beginning or end of the string. - elem_cnt++; - } - } - uint32_t table_size = elem_cnt + 2; // Last element is the whole string - - char **list = calloc(table_size, sizeof(char*)); - uint32_t count = 0; - while ((split_at = strstr(str_iter, delim)) != NULL) { - unsigned sub_len = (split_at - str_iter); - list[count] = calloc(sub_len + 1, sizeof(char)); - memcpy(list[count], str_iter, sub_len); - count++; - str_iter += sub_len + strlen(delim); + +char **split(char *str, char *delim, int *size) +{ + char **result; + char *token, *src; + int cnt; + + cnt = 0; + src = str; + result = NULL; + + while ((token = strstr(src, delim)) != NULL) { + result = (char **)realloc(result, sizeof(char *) * (cnt + 1)); + result[cnt] = (char *)calloc(1, sizeof(char) * (int)(token - src + 10)); + memcpy(result[cnt], src, token - src); + result[cnt][token - src] = '\0'; + src = token + strlen(delim); + cnt ++; } - if (strlen(str) > 0) { - list[count] = strdup(str); - count++; + if (strlen(src) > 0) { + result = (char **)realloc(result, sizeof(char *) * (cnt + 1)); + result[cnt] = strdup(src); + cnt ++; } - *size = count; - return list; + + *size = cnt; + return result; } void print_strs(char **list_str, int size)