From a84650ebd5ac2cbb49f14eb7c98736a3f8215bf1 Mon Sep 17 00:00:00 2001 From: klementtan Date: Thu, 17 Feb 2022 03:48:44 +0800 Subject: util: Fix ReadBinaryFile reading beyond maxsize --- src/test/util_tests.cpp | 49 +++++++++++++++++++++++++++++++++++++++++++++- src/util/readwritefile.cpp | 4 ++-- 2 files changed, 50 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/test/util_tests.cpp b/src/test/util_tests.cpp index 52b24327ec..f9f1a2f2e9 100644 --- a/src/test/util_tests.cpp +++ b/src/test/util_tests.cpp @@ -17,6 +17,7 @@ #include // For MessageSign(), MessageVerify(), MESSAGE_MAGIC #include #include +#include #include #include #include @@ -24,9 +25,10 @@ #include #include -#include +#include #include #include +#include #include #include #include @@ -2576,4 +2578,49 @@ BOOST_AUTO_TEST_CASE(util_ParseByteUnits) BOOST_CHECK(!ParseByteUnits("1x", noop)); } +BOOST_AUTO_TEST_CASE(util_ReadBinaryFile) +{ + fs::path tmpfolder = m_args.GetDataDirBase(); + fs::path tmpfile = tmpfolder / "read_binary.dat"; + std::string expected_text; + for (int i = 0; i < 30; i++) { + expected_text += "0123456789"; + } + { + std::ofstream file{tmpfile}; + file << expected_text; + } + { + // read all contents in file + auto [valid, text] = ReadBinaryFile(tmpfile); + BOOST_CHECK(valid); + BOOST_CHECK_EQUAL(text, expected_text); + } + { + // read half contents in file + auto [valid, text] = ReadBinaryFile(tmpfile, expected_text.size() / 2); + BOOST_CHECK(valid); + BOOST_CHECK_EQUAL(text, expected_text.substr(0, expected_text.size() / 2)); + } + { + // read from non-existent file + fs::path invalid_file = tmpfolder / "invalid_binary.dat"; + auto [valid, text] = ReadBinaryFile(invalid_file); + BOOST_CHECK(!valid); + BOOST_CHECK(text.empty()); + } +} + +BOOST_AUTO_TEST_CASE(util_WriteBinaryFile) +{ + fs::path tmpfolder = m_args.GetDataDirBase(); + fs::path tmpfile = tmpfolder / "write_binary.dat"; + std::string expected_text = "bitcoin"; + auto valid = WriteBinaryFile(tmpfile, expected_text); + std::string actual_text; + std::ifstream file{tmpfile}; + file >> actual_text; + BOOST_CHECK(valid); + BOOST_CHECK_EQUAL(actual_text, expected_text); +} BOOST_AUTO_TEST_SUITE_END() 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 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 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); } -- cgit v1.2.3