Skip to content

Commit ca8fb59

Browse files
committed
wallet: Warn on unexpected EOF while salvaging wallet
Check for EOF before every getline, and warn when reading gets to EOF before the end of the data. Stricter error checking could shed more light on issues such as bitcoin#7463 and bitcoin#7379.
1 parent bf1e113 commit ca8fb59

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

src/wallet/db.cpp

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,11 @@ CDBEnv::VerifyResult CDBEnv::Verify(const std::string& strFile, bool (*recoverFu
165165
return (fRecovered ? RECOVER_OK : RECOVER_FAIL);
166166
}
167167

168+
/* End of headers, beginning of key/value data */
169+
static const char *HEADER_END = "HEADER=END";
170+
/* End of key/value data */
171+
static const char *DATA_END = "DATA=END";
172+
168173
bool CDBEnv::Salvage(const std::string& strFile, bool fAggressive, std::vector<CDBEnv::KeyValPair>& vResult)
169174
{
170175
LOCK(cs_db);
@@ -199,18 +204,29 @@ bool CDBEnv::Salvage(const std::string& strFile, bool fAggressive, std::vector<C
199204
// DATA=END
200205

201206
string strLine;
202-
while (!strDump.eof() && strLine != "HEADER=END")
207+
while (!strDump.eof() && strLine != HEADER_END)
203208
getline(strDump, strLine); // Skip past header
204209

205210
std::string keyHex, valueHex;
206-
while (!strDump.eof() && keyHex != "DATA=END") {
211+
while (!strDump.eof() && keyHex != DATA_END) {
207212
getline(strDump, keyHex);
208-
if (keyHex != "DATA=END") {
213+
if (keyHex != DATA_END) {
214+
if (strDump.eof())
215+
break;
209216
getline(strDump, valueHex);
217+
if (valueHex == DATA_END) {
218+
LogPrintf("CDBEnv::Salvage: WARNING: Number of keys in data does not match number of values.\n");
219+
break;
220+
}
210221
vResult.push_back(make_pair(ParseHex(keyHex), ParseHex(valueHex)));
211222
}
212223
}
213224

225+
if (keyHex != DATA_END) {
226+
LogPrintf("CDBEnv::Salvage: WARNING: Unexpected end of file while reading salvage output.\n");
227+
return false;
228+
}
229+
214230
return (result == 0);
215231
}
216232

0 commit comments

Comments
 (0)