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 ++++++++++++++++------------ src/utils/file.h | 2 ++ src/utils/string.c | 1 + 3 files changed, 19 insertions(+), 12 deletions(-) (limited to 'src/utils') 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; } diff --git a/src/utils/file.h b/src/utils/file.h index 4a904e4..31e6503 100644 --- a/src/utils/file.h +++ b/src/utils/file.h @@ -10,4 +10,6 @@ extern size_t fileCodes_length; void fileInit(); void fileDelete(); +void filePush(const char *filePath,char *code); + char *readWholeFile(const char *filePath); diff --git a/src/utils/string.c b/src/utils/string.c index 5f9b7de..a5baab4 100644 --- a/src/utils/string.c +++ b/src/utils/string.c @@ -1,5 +1,6 @@ #include "string.h" #include "memory.h" +#include "utils/log.h" #include #include -- cgit v1.2.3