Merge pull request #290 from mo4islona/fix/read-config

Read config size through fseek instead lstat
This commit is contained in:
Andrey Borodin 2021-03-07 20:34:34 +05:00 committed by GitHub
commit 5bbbae19c7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 19 additions and 20 deletions

View File

@ -213,29 +213,28 @@ static int od_config_reader_open(od_config_reader_t *reader, char *config_file)
{ {
reader->config_file = config_file; reader->config_file = config_file;
/* read file */ /* read file */
struct stat st;
int rc = lstat(config_file, &st);
if (rc == -1)
goto error;
char *config_buf = NULL; char *config_buf = NULL;
if (st.st_size > 0) { FILE *file = fopen(config_file, "r");
config_buf = malloc(st.st_size); if (file == NULL)
if (config_buf == NULL) goto error;
goto error; fseek(file, 0, SEEK_END);
FILE *file = fopen(config_file, "r"); int size = (int)ftell(file);
if (file == NULL) { if (size == -1)
free(config_buf); goto error;
goto error; fseek(file, 0, SEEK_SET);
} config_buf = malloc(size);
rc = fread(config_buf, st.st_size, 1, file); if (config_buf == NULL)
fclose(file); goto error;
if (rc != 1) { int rc = fread(config_buf, size, 1, file);
free(config_buf); fclose(file);
goto error; if (rc != 1) {
} free(config_buf);
goto error;
} }
reader->data = config_buf; reader->data = config_buf;
reader->data_size = st.st_size; reader->data_size = size;
od_parser_init(&reader->parser, reader->data, reader->data_size); od_parser_init(&reader->parser, reader->data, reader->data_size);
return 0; return 0;
error: error: