aboutsummaryrefslogtreecommitdiff
path: root/src/util/readwritefile.cpp
diff options
context:
space:
mode:
authorklementtan <klementtan@gmail.com>2022-02-17 03:48:44 +0800
committerklementtan <klementtan@gmail.com>2022-02-19 18:39:43 +0800
commita84650ebd5ac2cbb49f14eb7c98736a3f8215bf1 (patch)
tree960416e8afc246282411ec4a85e01932906bce95 /src/util/readwritefile.cpp
parent25a91a571a4f3453f7e0e9299ee7a40a61d04f19 (diff)
downloadbitcoin-a84650ebd5ac2cbb49f14eb7c98736a3f8215bf1.tar.xz
util: Fix ReadBinaryFile reading beyond maxsize
Diffstat (limited to 'src/util/readwritefile.cpp')
-rw-r--r--src/util/readwritefile.cpp4
1 files changed, 2 insertions, 2 deletions
diff --git a/src/util/readwritefile.cpp b/src/util/readwritefile.cpp
index a45c41d367..628e6a3980 100644
--- a/src/util/readwritefile.cpp
+++ b/src/util/readwritefile.cpp
@@ -18,7 +18,7 @@ std::pair<bool,std::string> ReadBinaryFile(const fs::path &filename, size_t maxs
std::string retval;
char buffer[128];
do {
- const size_t n = fread(buffer, 1, sizeof(buffer), f);
+ const size_t n = fread(buffer, 1, std::min(sizeof(buffer), maxsize - retval.size()), f);
// Check for reading errors so we don't return any data if we couldn't
// read the entire file (or up to maxsize)
if (ferror(f)) {
@@ -26,7 +26,7 @@ std::pair<bool,std::string> ReadBinaryFile(const fs::path &filename, size_t maxs
return std::make_pair(false,"");
}
retval.append(buffer, buffer+n);
- } while (!feof(f) && retval.size() <= maxsize);
+ } while (!feof(f) && retval.size() < maxsize);
fclose(f);
return std::make_pair(true,retval);
}