From ebbfc3ae8df85c351002000a76900e3086375e7b Mon Sep 17 00:00:00 2001 From: Stefan Weil Date: Fri, 10 Nov 2017 15:46:38 +0100 Subject: [PATCH] Improve robustness of function LoadDataFromFile (#1207) ftell returns a long value which can be negative when an error occurred. It returns LONG_MAX for directories. Both cases were not handled by the old code. Signed-off-by: Stefan Weil --- ccutil/genericvector.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/ccutil/genericvector.h b/ccutil/genericvector.h index 3c37b4aa..a987eed2 100644 --- a/ccutil/genericvector.h +++ b/ccutil/genericvector.h @@ -370,9 +370,10 @@ inline bool LoadDataFromFile(const char* filename, GenericVector* data) { FILE* fp = fopen(filename, "rb"); if (fp != NULL) { fseek(fp, 0, SEEK_END); - size_t size = ftell(fp); + long size = ftell(fp); fseek(fp, 0, SEEK_SET); - if (size > 0) { + // Trying to open a directory on Linux sets size to LONG_MAX. Catch it here. + if (size > 0 && size < LONG_MAX) { data->resize_no_init(size); result = fread(&(*data)[0], 1, size, fp) == size; }