From 83b22bff6cb4ddc3ff626228e3cf454a25237d71 Mon Sep 17 00:00:00 2001 From: Bastian Dehn Date: Fri, 14 Feb 2025 09:27:05 +0100 Subject: [PATCH] change config read with size first --- src/kds_s2000w_config.c | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/src/kds_s2000w_config.c b/src/kds_s2000w_config.c index 4c8a811..e793107 100644 --- a/src/kds_s2000w_config.c +++ b/src/kds_s2000w_config.c @@ -18,28 +18,20 @@ void _kds_s2000w_config_copy_string_to_config_value(cfg_t* cfg, const char* key, char* kds_s2000w_config_read(const char* filename) { FILE* config_file = fopen(filename, "r"); - char* buf = malloc(sizeof(char)); - char** mem = malloc(sizeof(char*)); - size_t* sizeloc = malloc(sizeof(size_t)); - FILE* config_stream = open_memstream(mem, sizeloc); + size_t config_size = 0; - while (fread(buf, sizeof(char), 1, config_file) > 0) { - fwrite(buf, sizeof(char), 1, config_stream); + char buf = 0; + while (fread(&buf, sizeof(char), 1, config_file) > 0) { + config_size++; } + fseek(config_file, 0, SEEK_SET); + + char* config_string = malloc(sizeof(char) * config_size + 1); + fread(config_string, config_size, 1, config_file); + config_string[config_size] = '\0'; fclose(config_file); config_file = NULL; - fclose(config_stream); - config_stream = NULL; - - char* config_string = *mem; - - free(buf); - buf = NULL; - free(sizeloc); - sizeloc = NULL; - free(mem); - mem = NULL; return config_string; } -- 2.39.5