Skip to content

Commit 8ae973c

Browse files
committed
Allocate more space if necessary in RandSeedAddPerfMon
Currently we use a fixed buffer of 250000 bytes to request HKEY_PERFORMANCE_DATA. In many cases this is not enough, causing the entropy collection to be skipped. Use a loop that grows the buffer as specified in the RegQueryValueEx documentation: http://msdn.microsoft.com/en-us/library/windows/desktop/ms724911%28v=vs.85%29.aspx (as the size of the performance data can differ for every call, the normal solution of requesting the size then allocating that can't work)
1 parent be873f6 commit 8ae973c

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/util.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,17 @@ void RandAddSeedPerfmon()
169169
// Don't need this on Linux, OpenSSL automatically uses /dev/urandom
170170
// Seed with the entire set of perfmon data
171171
std::vector <unsigned char> vData(250000,0);
172-
unsigned long nSize = vData.size();
173-
long ret = RegQueryValueExA(HKEY_PERFORMANCE_DATA, "Global", NULL, NULL, begin_ptr(vData), &nSize);
172+
long ret = 0;
173+
unsigned long nSize = 0;
174+
const size_t nMaxSize = 10000000; // Bail out at more than 10MB of performance data
175+
while (true)
176+
{
177+
nSize = vData.size();
178+
ret = RegQueryValueExA(HKEY_PERFORMANCE_DATA, "Global", NULL, NULL, begin_ptr(vData), &nSize);
179+
if (ret != ERROR_MORE_DATA || vData.size() >= nMaxSize)
180+
break;
181+
vData.resize(std::max((vData.size()*3)/2, nMaxSize)); // Grow size of buffer exponentially
182+
}
174183
RegCloseKey(HKEY_PERFORMANCE_DATA);
175184
if (ret == ERROR_SUCCESS)
176185
{

0 commit comments

Comments
 (0)