From 10d44e7ff547a806e4bf109e7aadd48488ddc9f0 Mon Sep 17 00:00:00 2001 From: frosty Date: Wed, 12 Aug 2026 15:40:41 -0400 Subject: fix: improvements allocation safety and string processing --- src/Utility/Utility.c | 88 +++++++++------------------------------------------ src/Utility/Utility.h | 8 ----- 2 files changed, 15 insertions(+), 81 deletions(-) (limited to 'src/Utility') diff --git a/src/Utility/Utility.c b/src/Utility/Utility.c index 97a8d15..5183efe 100644 --- a/src/Utility/Utility.c +++ b/src/Utility/Utility.c @@ -14,7 +14,6 @@ static int themes_initialized = 0; void init_themes(const char *static_path) { if (themes_initialized) return; - themes_initialized = 1; char themes_dir[512]; snprintf(themes_dir, sizeof(themes_dir), "%s/themes", static_path); @@ -26,20 +25,32 @@ void init_themes(const char *static_path) { struct dirent *entry; int capacity = 4; themes_list = malloc(sizeof(char *) * capacity); + if (!themes_list) { + closedir(dir); + return; + } themes_count = 0; while ((entry = readdir(dir)) != NULL) { size_t len = strlen(entry->d_name); if (len > 4 && strcmp(entry->d_name + len - 4, ".css") == 0) { if (themes_count >= capacity) { - capacity *= 2; - themes_list = realloc(themes_list, sizeof(char *) * capacity); + int new_capacity = capacity * 2; + char **new_list = realloc(themes_list, sizeof(char *) * new_capacity); + if (!new_list) + break; + themes_list = new_list; + capacity = new_capacity; } - themes_list[themes_count] = strndup(entry->d_name, len - 4); + char *theme = strndup(entry->d_name, len - 4); + if (!theme) + break; + themes_list[themes_count] = theme; themes_count++; } } closedir(dir); + themes_initialized = 1; for (int i = 0; i < themes_count; i++) { for (int j = i + 1; j < themes_count; j++) { @@ -206,50 +217,6 @@ int user_engines_contains(const char *engine_id, char **ids, int count) { return 0; } -int append_string_row(char ****matrix, int **inner_counts, int row_count, - const char *const *values, int field_count) { - if (!matrix || !inner_counts || !values || field_count <= 0) - return row_count; - - char **row = calloc((size_t)field_count, sizeof(*row)); - if (!row) - return row_count; - - for (int i = 0; i < field_count; i++) { - row[i] = strdup(values[i] ? values[i] : ""); - if (!row[i]) { - for (int j = 0; j < i; j++) - free(row[j]); - free(row); - return row_count; - } - } - - char ***new_matrix = malloc(sizeof(*new_matrix) * (row_count + 1)); - int *new_counts = malloc(sizeof(*new_counts) * (row_count + 1)); - if (!new_matrix || !new_counts) { - free(new_matrix); - free(new_counts); - for (int i = 0; i < field_count; i++) - free(row[i]); - free(row); - return row_count; - } - - if (row_count > 0) { - memcpy(new_matrix, *matrix, sizeof(*new_matrix) * row_count); - memcpy(new_counts, *inner_counts, sizeof(*new_counts) * row_count); - } - - new_matrix[row_count] = row; - new_counts[row_count] = field_count; - free(*matrix); - free(*inner_counts); - *matrix = new_matrix; - *inner_counts = new_counts; - return row_count + 1; -} - void free_string_matrix(char ***matrix, int *inner_counts, int row_count) { if (matrix) { for (int i = 0; i < row_count; i++) { @@ -324,31 +291,6 @@ void string_matrix_free(StringMatrix *matrix) { *matrix = (StringMatrix){0}; } -int add_link_to_collection(const char *href, const char *label, - const char *class_name, char ****collection, - int **inner_counts, int current_count) { - const char *values[LINK_FIELD_COUNT] = {href, label, class_name}; - return append_string_row(collection, inner_counts, current_count, values, - LINK_FIELD_COUNT); -} - -int build_pagination(int page, char *(*href_builder)(int page, void *data), - void *data, char ****out_matrix, int **out_inner_counts) { - if (!out_matrix || !out_inner_counts) - return 0; - - StringMatrix matrix; - if (string_matrix_build_pagination(page, href_builder, data, &matrix) != 0) { - *out_matrix = NULL; - *out_inner_counts = NULL; - return 0; - } - - *out_matrix = matrix.rows; - *out_inner_counts = matrix.field_counts; - return matrix.count; -} - int string_matrix_build_pagination(int page, char *(*href_builder)(int page, void *data), void *data, StringMatrix *out_matrix) { diff --git a/src/Utility/Utility.h b/src/Utility/Utility.h index 663f2c4..a78fe65 100644 --- a/src/Utility/Utility.h +++ b/src/Utility/Utility.h @@ -31,8 +31,6 @@ int is_engine_id_enabled(const char *engine_id); int get_user_engines(char ***out_ids, int *out_count); int user_engines_contains(const char *engine_id, char **ids, int count); -int append_string_row(char ****matrix, int **inner_counts, int row_count, - const char *const *values, int field_count); void free_string_matrix(char ***matrix, int *inner_counts, int row_count); void string_matrix_init(StringMatrix *matrix); @@ -40,12 +38,6 @@ int string_matrix_append(StringMatrix *matrix, const char *const *values, int field_count); void string_matrix_free(StringMatrix *matrix); -int add_link_to_collection(const char *href, const char *label, - const char *class_name, char ****collection, - int **inner_counts, int current_count); - -int build_pagination(int page, char *(*href_builder)(int page, void *data), - void *data, char ****out_matrix, int **out_inner_counts); int string_matrix_build_pagination(int page, char *(*href_builder)(int page, void *data), void *data, StringMatrix *out_matrix); -- cgit v1.3