CAddrMan: verify pchMessageStart file marker, before reading address data

This avoids the case where you read the address data, before noticing it is
the wrong network.
This commit is contained in:
Jeff Garzik 2012-10-08 13:41:57 -04:00 committed by Jeff Garzik
parent a6be58d536
commit bd2e140571

View file

@ -842,20 +842,22 @@ bool CAddrDB::Read(CAddrMan& addr)
if (hashIn != hashTmp)
return error("CAddrman::Read() : checksum mismatch; data corrupted");
// de-serialize address data
unsigned char pchMsgTmp[4];
try {
// de-serialize file header (pchMessageStart magic number) and
ssPeers >> FLATDATA(pchMsgTmp);
// verify the network matches ours
if (memcmp(pchMsgTmp, pchMessageStart, sizeof(pchMsgTmp)))
return error("CAddrman::Read() : invalid network magic number");
// de-serialize address data into one CAddrMan object
ssPeers >> addr;
}
catch (std::exception &e) {
return error("CAddrman::Read() : I/O error or stream data corrupted");
}
// finally, verify the network matches ours
if (memcmp(pchMsgTmp, pchMessageStart, sizeof(pchMsgTmp)))
return error("CAddrman::Read() : invalid network magic number");
return true;
}