Skip to content

Commit eacff4a

Browse files
committed
Merge pull request #4392
8ae973c Allocate more space if necessary in RandSeedAddPerfMon (Wladimir J. van der Laan) be873f6 Issue warning if collecting RandSeed data failed (Wladimir J. van der Laan) fcb0a1b change "char pch[200000]" to "new char[200000]" (daniel)
2 parents ffb32ac + 8ae973c commit eacff4a

File tree

1 file changed

+26
-11
lines changed

1 file changed

+26
-11
lines changed

src/util.cpp

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -167,16 +167,31 @@ void RandAddSeedPerfmon()
167167
#ifdef WIN32
168168
// Don't need this on Linux, OpenSSL automatically uses /dev/urandom
169169
// Seed with the entire set of perfmon data
170-
unsigned char pdata[250000];
171-
memset(pdata, 0, sizeof(pdata));
172-
unsigned long nSize = sizeof(pdata);
173-
long ret = RegQueryValueExA(HKEY_PERFORMANCE_DATA, "Global", NULL, NULL, pdata, &nSize);
170+
std::vector <unsigned char> vData(250000,0);
171+
long ret = 0;
172+
unsigned long nSize = 0;
173+
const size_t nMaxSize = 10000000; // Bail out at more than 10MB of performance data
174+
while (true)
175+
{
176+
nSize = vData.size();
177+
ret = RegQueryValueExA(HKEY_PERFORMANCE_DATA, "Global", NULL, NULL, begin_ptr(vData), &nSize);
178+
if (ret != ERROR_MORE_DATA || vData.size() >= nMaxSize)
179+
break;
180+
vData.resize(std::max((vData.size()*3)/2, nMaxSize)); // Grow size of buffer exponentially
181+
}
174182
RegCloseKey(HKEY_PERFORMANCE_DATA);
175183
if (ret == ERROR_SUCCESS)
176184
{
177-
RAND_add(pdata, nSize, nSize/100.0);
178-
OPENSSL_cleanse(pdata, nSize);
179-
LogPrint("rand", "RandAddSeed() %lu bytes\n", nSize);
185+
RAND_add(begin_ptr(vData), nSize, nSize/100.0);
186+
OPENSSL_cleanse(begin_ptr(vData), nSize);
187+
LogPrint("rand", "%s: %lu bytes\n", __func__, nSize);
188+
} else {
189+
static bool warned = false; // Warn only once
190+
if (!warned)
191+
{
192+
LogPrintf("%s: Warning: RegQueryValueExA(HKEY_PERFORMANCE_DATA) failed with code %i\n", __func__, ret);
193+
warned = true;
194+
}
180195
}
181196
#endif
182197
}
@@ -1140,15 +1155,15 @@ void ShrinkDebugFile()
11401155
if (file && boost::filesystem::file_size(pathLog) > 10 * 1000000)
11411156
{
11421157
// Restart the file with some of the end
1143-
char pch[200000];
1144-
fseek(file, -sizeof(pch), SEEK_END);
1145-
int nBytes = fread(pch, 1, sizeof(pch), file);
1158+
std::vector <char> vch(200000,0);
1159+
fseek(file, -vch.size(), SEEK_END);
1160+
int nBytes = fread(begin_ptr(vch), 1, vch.size(), file);
11461161
fclose(file);
11471162

11481163
file = fopen(pathLog.string().c_str(), "w");
11491164
if (file)
11501165
{
1151-
fwrite(pch, 1, nBytes, file);
1166+
fwrite(begin_ptr(vch), 1, nBytes, file);
11521167
fclose(file);
11531168
}
11541169
}

0 commit comments

Comments
 (0)