@@ -167,16 +167,31 @@ void RandAddSeedPerfmon()
167
167
#ifdef WIN32
168
168
// Don't need this on Linux, OpenSSL automatically uses /dev/urandom
169
169
// 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
+ }
174
182
RegCloseKey (HKEY_PERFORMANCE_DATA);
175
183
if (ret == ERROR_SUCCESS)
176
184
{
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
+ }
180
195
}
181
196
#endif
182
197
}
@@ -1140,15 +1155,15 @@ void ShrinkDebugFile()
1140
1155
if (file && boost::filesystem::file_size (pathLog) > 10 * 1000000 )
1141
1156
{
1142
1157
// 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);
1146
1161
fclose (file);
1147
1162
1148
1163
file = fopen (pathLog.string ().c_str (), " w" );
1149
1164
if (file)
1150
1165
{
1151
- fwrite (pch , 1 , nBytes, file);
1166
+ fwrite (begin_ptr (vch) , 1 , nBytes, file);
1152
1167
fclose (file);
1153
1168
}
1154
1169
}
0 commit comments