mirror of
https://github.com/tesseract-ocr/tesseract.git
synced 2024-11-24 11:09:06 +08:00
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>
This commit is contained in:
parent
f3c4b894dc
commit
ebbfc3ae8d
@ -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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user