Permalink
Browse files

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 <sw@weilnetz.de>
  • Loading branch information...
stweil authored and zdenop committed Nov 10, 2017
1 parent f3c4b89 commit ebbfc3ae8df85c351002000a76900e3086375e7b
Showing with 3 additions and 2 deletions.
  1. +3 −2 ccutil/genericvector.h
View
@@ -370,9 +370,10 @@ inline bool LoadDataFromFile(const char* filename, GenericVector<char>* 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;
}

0 comments on commit ebbfc3a

Please sign in to comment.