aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorfanquake <fanquake@gmail.com>2020-10-19 13:28:11 +0800
committerfanquake <fanquake@gmail.com>2020-10-19 14:04:10 +0800
commit62af467ff03941f3cf73c9727dd8cdb11ce20b3d (patch)
tree68a56b3c9df9a8ce8c13b280c0ab18fc0bd7b431 /src
parenta1e0359618e3a66d0156d2b0a4702bc55f957a3f (diff)
parentbd5215103eb3985c1622eddea45a040e6173829c (diff)
downloadbitcoin-62af467ff03941f3cf73c9727dd8cdb11ce20b3d.tar.xz
Merge #20082: [bugfix] random: fixes read buffer to use min rather than max
bd5215103eb3985c1622eddea45a040e6173829c random: fixes read buffer resizing in RandAddSeedPerfmon (Ethan Heilman) Pull request description: As shown below when resizing the read buffer `vData` `std::max((vData.size() * 3) / 2, nMaxSize)` is used. This means that the buffer size immediately jumps to `nMaxSize`. I believe the intend of this code is to grow the buffer size through several steps rather than immediately resize it to the max size. ```cpp std::vector<unsigned char> vData(250000, 0); long ret = 0; unsigned long nSize = 0; const size_t nMaxSize = 10000000; // Bail out at more than 10MB of performance data while (true) { nSize = vData.size(); ret = RegQueryValueExA(HKEY_PERFORMANCE_DATA, "Global", nullptr, nullptr, vData.data(), &nSize); if (ret != ERROR_MORE_DATA || vData.size() >= nMaxSize) break; vData.resize(std::max((vData.size() * 3) / 2, nMaxSize)); // Grow size of buffer exponentially } ``` vData always starts at size 250,000 and nMaxSize is always 10,000,000 so the first time this line is reached: ```cpp vData.resize(std::max((vData.size() * 3) / 2, nMaxSize)); ``` the effect will always be to resize vData to nMaxSize. Then because the loop terminates when vData.size >= 10,000,000 only one resize operation will take place. To fix this issue we replace `std::min` with `std::max` This PR also adds a comment clarifying the behavior of this function the first time it is called. ACKs for top commit: fanquake: ACK bd5215103eb3985c1622eddea45a040e6173829c - thanks for taking a look at this Ethan. Swapping from `std::max` to `std::min` here certainly seems correct. Tree-SHA512: 7c65f700e5bbe44bc2f1ffdcdc99ec19c542894c95b5ee9791facd09d02afae88d1f8f35af129719e4860db94bc790856e7adb1d218a395381e7c2913b95f1d0
Diffstat (limited to 'src')
-rw-r--r--src/randomenv.cpp5
1 files changed, 3 insertions, 2 deletions
diff --git a/src/randomenv.cpp b/src/randomenv.cpp
index 073d82b491..07122b7f6d 100644
--- a/src/randomenv.cpp
+++ b/src/randomenv.cpp
@@ -67,7 +67,8 @@ void RandAddSeedPerfmon(CSHA512& hasher)
#ifdef WIN32
// Seed with the entire set of perfmon data
- // This can take up to 2 seconds, so only do it every 10 minutes
+ // This can take up to 2 seconds, so only do it every 10 minutes.
+ // Initialize last_perfmon to 0 seconds, we don't skip the first call.
static std::atomic<std::chrono::seconds> last_perfmon{std::chrono::seconds{0}};
auto last_time = last_perfmon.load();
auto current_time = GetTime<std::chrono::seconds>();
@@ -83,7 +84,7 @@ void RandAddSeedPerfmon(CSHA512& hasher)
ret = RegQueryValueExA(HKEY_PERFORMANCE_DATA, "Global", nullptr, nullptr, vData.data(), &nSize);
if (ret != ERROR_MORE_DATA || vData.size() >= nMaxSize)
break;
- vData.resize(std::max((vData.size() * 3) / 2, nMaxSize)); // Grow size of buffer exponentially
+ vData.resize(std::min((vData.size() * 3) / 2, nMaxSize)); // Grow size of buffer exponentially
}
RegCloseKey(HKEY_PERFORMANCE_DATA);
if (ret == ERROR_SUCCESS) {