diff options
author | fanquake <fanquake@gmail.com> | 2020-10-19 13:28:11 +0800 |
---|---|---|
committer | fanquake <fanquake@gmail.com> | 2020-10-19 14:04:10 +0800 |
commit | 62af467ff03941f3cf73c9727dd8cdb11ce20b3d (patch) | |
tree | 68a56b3c9df9a8ce8c13b280c0ab18fc0bd7b431 /src/test/fuzz/fuzz.cpp | |
parent | a1e0359618e3a66d0156d2b0a4702bc55f957a3f (diff) | |
parent | bd5215103eb3985c1622eddea45a040e6173829c (diff) |
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/test/fuzz/fuzz.cpp')
0 files changed, 0 insertions, 0 deletions