diff --git a/src/tools/getdns_query.c b/src/tools/getdns_query.c index ce64a679..380a21c7 100644 --- a/src/tools/getdns_query.c +++ b/src/tools/getdns_query.c @@ -543,6 +543,7 @@ int parse_config_file(const char *fn, int report_open_failure) FILE *fh; char *config_file = NULL; long config_file_sz; + size_t read_sz; if (!(fh = fopen(fn, "r"))) { if (report_open_failure) @@ -567,13 +568,14 @@ int parse_config_file(const char *fn, int report_open_failure) return GETDNS_RETURN_MEMORY_ERROR; } rewind(fh); - if (fread(config_file, 1, config_file_sz, fh) != (size_t)config_file_sz) { + read_sz = fread(config_file, 1, config_file_sz + 1, fh); + if (read_sz > config_file_sz || ferror(fh) || !feof(fh)) { fprintf( stderr, "An error occurred while reading \"%s\": %s\n" , fn, strerror(errno)); fclose(fh); return GETDNS_RETURN_MEMORY_ERROR; } - config_file[config_file_sz] = 0; + config_file[read_sz] = 0; fclose(fh); parse_config(config_file, strstr(fn, ".yml") != NULL); free(config_file);