Skip to content

Commit ebbfc3a

Browse files
stweilzdenop
authored andcommitted
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>
1 parent f3c4b89 commit ebbfc3a

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

ccutil/genericvector.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -370,9 +370,10 @@ inline bool LoadDataFromFile(const char* filename, GenericVector<char>* data) {
370370
FILE* fp = fopen(filename, "rb");
371371
if (fp != NULL) {
372372
fseek(fp, 0, SEEK_END);
373-
size_t size = ftell(fp);
373+
long size = ftell(fp);
374374
fseek(fp, 0, SEEK_SET);
375-
if (size > 0) {
375+
// Trying to open a directory on Linux sets size to LONG_MAX. Catch it here.
376+
if (size > 0 && size < LONG_MAX) {
376377
data->resize_no_init(size);
377378
result = fread(&(*data)[0], 1, size, fp) == size;
378379
}

0 commit comments

Comments
 (0)