From 21f0661757a8b32caf47dfc714ca8fa9baab6679 Mon Sep 17 00:00:00 2001 From: Eugene Formanenko Date: Sun, 7 Mar 2021 07:06:42 +0300 Subject: [PATCH 1/2] Use fseek instead lstat when reading config --- sources/config_reader.c | 39 +++++++++++++++++++-------------------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/sources/config_reader.c b/sources/config_reader.c index fb5fd311..e0503618 100644 --- a/sources/config_reader.c +++ b/sources/config_reader.c @@ -213,29 +213,28 @@ static int od_config_reader_open(od_config_reader_t *reader, char *config_file) { reader->config_file = config_file; /* read file */ - struct stat st; - int rc = lstat(config_file, &st); - if (rc == -1) - goto error; char *config_buf = NULL; - if (st.st_size > 0) { - config_buf = malloc(st.st_size); - if (config_buf == NULL) - goto error; - FILE *file = fopen(config_file, "r"); - if (file == NULL) { - free(config_buf); - goto error; - } - rc = fread(config_buf, st.st_size, 1, file); - fclose(file); - if (rc != 1) { - free(config_buf); - goto error; - } + FILE *file = fopen(config_file, "r"); + if (file == NULL) + goto error; + fseek(file, 0, SEEK_END); + int size = (int) ftell(file); + if (size == -1) + goto error; + fseek(file, 0, SEEK_SET); + config_buf = malloc(size); + if (config_buf == NULL) + goto error; + int rc = fread(config_buf, size, 1, file); + fclose(file); + if (rc != 1) { + free(config_buf); + goto error; } + reader->data = config_buf; - reader->data_size = st.st_size; + reader->data_size = size; + od_parser_init(&reader->parser, reader->data, reader->data_size); return 0; error: From 001f0f4eccafd930e7a8ab1e6e3cffd61d2071e7 Mon Sep 17 00:00:00 2001 From: Andrey Borodin Date: Sun, 7 Mar 2021 18:30:06 +0500 Subject: [PATCH 2/2] Apply fmt --- sources/config_reader.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sources/config_reader.c b/sources/config_reader.c index e0503618..2caa848c 100644 --- a/sources/config_reader.c +++ b/sources/config_reader.c @@ -218,7 +218,7 @@ static int od_config_reader_open(od_config_reader_t *reader, char *config_file) if (file == NULL) goto error; fseek(file, 0, SEEK_END); - int size = (int) ftell(file); + int size = (int)ftell(file); if (size == -1) goto error; fseek(file, 0, SEEK_SET);