Skip to content

Commit 8b6e4b3

Browse files
committed
util: fix ReadBinaryFile() returning partial contents
If an error occurs and `fread()` returns `0` (nothing was read) then the code before this patch would have returned "success" with a partially read contents of the file.
1 parent 4cba2fd commit 8b6e4b3

File tree

1 file changed

+3
-5
lines changed

1 file changed

+3
-5
lines changed

src/util/readwritefile.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,16 @@ std::pair<bool,std::string> ReadBinaryFile(const fs::path &filename, size_t maxs
1717
return std::make_pair(false,"");
1818
std::string retval;
1919
char buffer[128];
20-
size_t n;
21-
while ((n=fread(buffer, 1, sizeof(buffer), f)) > 0) {
20+
do {
21+
const size_t n = fread(buffer, 1, sizeof(buffer), f);
2222
// Check for reading errors so we don't return any data if we couldn't
2323
// read the entire file (or up to maxsize)
2424
if (ferror(f)) {
2525
fclose(f);
2626
return std::make_pair(false,"");
2727
}
2828
retval.append(buffer, buffer+n);
29-
if (retval.size() > maxsize)
30-
break;
31-
}
29+
} while (!feof(f) && retval.size() <= maxsize);
3230
fclose(f);
3331
return std::make_pair(true,retval);
3432
}

0 commit comments

Comments
 (0)