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.
0 std::vector<unsigned char> vData(250000, 0);
1 long ret = 0;
2 unsigned long nSize = 0;
3 const size_t nMaxSize = 10000000; // Bail out at more than 10MB of performance data
4 while (true) {
5 nSize = vData.size();
6 ret = RegQueryValueExA(HKEY_PERFORMANCE_DATA, "Global", nullptr, nullptr, vData.data(), &nSize);
7 if (ret != ERROR_MORE_DATA || vData.size() >= nMaxSize)
8 break;
9 vData.resize(std::max((vData.size() * 3) / 2, nMaxSize)); // Grow size of buffer exponentially
10 }
vData always starts at size 250,000 and nMaxSize is always 10,000,000 so the first time this line is reached:
0vData.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.