aboutsummaryrefslogtreecommitdiff
path: root/src/util
diff options
context:
space:
mode:
authorMarcoFalke <falke.marco@gmail.com>2022-03-10 10:08:43 +0100
committerMarcoFalke <falke.marco@gmail.com>2022-03-10 10:24:05 +0100
commit5e33620ad81363d817a0050f29c68e4c3cf594b5 (patch)
treeb593c7687e20ffff3d842a960ef644eb19a1dca4 /src/util
parent430acb7d2a3272977d5a66bf9a09bcd55ffd15cc (diff)
parenta84650ebd5ac2cbb49f14eb7c98736a3f8215bf1 (diff)
downloadbitcoin-5e33620ad81363d817a0050f29c68e4c3cf594b5.tar.xz
Merge bitcoin/bitcoin#24371: util: Fix `ReadBinaryFile` reading beyond maxsize
a84650ebd5ac2cbb49f14eb7c98736a3f8215bf1 util: Fix ReadBinaryFile reading beyond maxsize (klementtan) Pull request description: Currently `ReadBinaryFile` will read beyond `maxsize` if `maxsize` is not a multiple of `128` (size of buffer) This is due to `fread` being called with `count = 128` instead of `count = min(128, maxsize - retval.size()` at every iteration The following unit test will fail: ```cpp BOOST_AUTO_TEST_CASE(util_ReadWriteFile) { fs::path tmpfolder = m_args.GetDataDirBase(); fs::path tmpfile = tmpfolder / "read_binary.dat"; std::string expected_text(300,'c'); { std::ofstream file{tmpfile}; file << expected_text; } { // read half the contents in file auto [valid, text] = ReadBinaryFile(tmpfile, expected_text.size() / 2); BOOST_CHECK_EQUAL(text.size(), 150); } } ``` Error: ``` test/util_tests.cpp:2593: error: in "util_tests/util_ReadWriteFile": check text.size() == 150 has failed [256 != 150] ``` ACKs for top commit: laanwj: Code review ACK a84650ebd5ac2cbb49f14eb7c98736a3f8215bf1 theStack: Code-review ACK a84650ebd5ac2cbb49f14eb7c98736a3f8215bf1 Tree-SHA512: 752eebe58bc2102dec199b6775f8c3304d899f0ce36d6a022a58e27b076ba945ccd572858b19137b769effd8c6de73a9277f641be24dfb17657fb7173ea0eda0
Diffstat (limited to 'src/util')
-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);
}