From 6d127edfa0162219c54b10e3496a3e9530f62792 Mon Sep 17 00:00:00 2001 From: A404M Date: Mon, 10 Feb 2025 13:03:25 +0330 Subject: fix double free of code some clean up --- src/utils/file.c | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) (limited to 'src/utils/file.c') diff --git a/src/utils/file.c b/src/utils/file.c index 89ccfa8..921befb 100644 --- a/src/utils/file.c +++ b/src/utils/file.c @@ -30,6 +30,21 @@ void fileDelete() { fileCodes_length = 0; } +void filePush(const char *filePath, char *code) { + if (fileCodes_capacity == fileCodes_length) { + fileCodes_capacity += fileCodes_capacity / 2 + 1; + fileCodes = + a404m_realloc(fileCodes, fileCodes_capacity * sizeof(*fileCodes)); + fileCodes_names = a404m_realloc( + fileCodes_names, fileCodes_capacity * sizeof(*fileCodes_names)); + } + fileCodes[fileCodes_length] = code; + fileCodes_names[fileCodes_length] = + a404m_malloc((strlen(filePath) + 1) * sizeof(**fileCodes_names)); + strcpy(fileCodes_names[fileCodes_length], filePath); + fileCodes_length += 1; +} + char *readWholeFile(const char *filePath) { FILE *file = fopen(filePath, "r"); @@ -48,18 +63,7 @@ char *readWholeFile(const char *filePath) { fclose(file); - if (fileCodes_capacity == fileCodes_length) { - fileCodes_capacity += fileCodes_capacity / 2 + 1; - fileCodes = - a404m_realloc(fileCodes, fileCodes_capacity * sizeof(*fileCodes)); - fileCodes_names = a404m_realloc( - fileCodes_names, fileCodes_capacity * sizeof(*fileCodes_names)); - } - fileCodes[fileCodes_length] = str; - fileCodes_names[fileCodes_length] = - a404m_malloc((strlen(filePath) + 1) * sizeof(**fileCodes_names)); - strcpy(fileCodes_names[fileCodes_length], filePath); - fileCodes_length += 1; + filePush(filePath, str); return str; } -- cgit v1.2.3