mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-11-11 22:50:59 +01:00
Fix de-serialization bug where AddrMan is corrupted after exception
* CAddrDB modified so that when de-serialization code throws an exception Addrman is reset to a clean state * CAddrDB modified to make unit tests possible * Regression test created to ensure bug is fixed * StartNode modifed to clear adrman if CAddrDB::Read returns an error code.
This commit is contained in:
committed by
Ethan Heilman
parent
326f010332
commit
1475ecf611
@@ -1944,6 +1944,7 @@ void StartNode(boost::thread_group& threadGroup, CScheduler& scheduler)
|
||||
if (adb.Read(addrman))
|
||||
LogPrintf("Loaded %i addresses from peers.dat %dms\n", addrman.size(), GetTimeMillis() - nStart);
|
||||
else {
|
||||
addrman.Clear(); // Addrman can be in an inconsistent state after failure, reset it
|
||||
LogPrintf("Invalid or missing peers.dat; recreating\n");
|
||||
DumpAddresses();
|
||||
}
|
||||
@@ -2336,6 +2337,11 @@ bool CAddrDB::Read(CAddrMan& addr)
|
||||
if (hashIn != hashTmp)
|
||||
return error("%s: Checksum mismatch, data corrupted", __func__);
|
||||
|
||||
return Read(addr, ssPeers);
|
||||
}
|
||||
|
||||
bool CAddrDB::Read(CAddrMan& addr, CDataStream& ssPeers)
|
||||
{
|
||||
unsigned char pchMsgTmp[4];
|
||||
try {
|
||||
// de-serialize file header (network specific magic number) and ..
|
||||
@@ -2349,6 +2355,8 @@ bool CAddrDB::Read(CAddrMan& addr)
|
||||
ssPeers >> addr;
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
// de-serialization has failed, ensure addrman is left in a clean state
|
||||
addr.Clear();
|
||||
return error("%s: Deserialize or I/O error - %s", __func__, e.what());
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user