Browse Source

Merge pull request #1381 from jgarzik/check-deser

Prevent crashes due to missing or corrupted database records
miguelfreitas
Jeff Garzik 13 years ago
parent
commit
5d0f7c4f47
  1. 14
      src/db.cpp
  2. 5
      src/db.h
  3. 5
      src/main.cpp
  4. 11
      src/main.h

14
src/db.cpp

@ -416,9 +416,15 @@ bool CTxDB::ReadOwnerTxes(uint160 hash160, int nMinHeight, vector<CTransaction>&
string strType; string strType;
uint160 hashItem; uint160 hashItem;
CDiskTxPos pos; CDiskTxPos pos;
ssKey >> strType >> hashItem >> pos;
int nItemHeight; int nItemHeight;
try {
ssKey >> strType >> hashItem >> pos;
ssValue >> nItemHeight; ssValue >> nItemHeight;
}
catch (std::exception &e) {
return error("%s() : deserialize error", __PRETTY_FUNCTION__);
}
// Read transaction // Read transaction
if (strType != "owner" || hashItem != hash160) if (strType != "owner" || hashItem != hash160)
@ -533,6 +539,8 @@ bool CTxDB::LoadBlockIndex()
return false; return false;
// Unserialize // Unserialize
try {
string strType; string strType;
ssKey >> strType; ssKey >> strType;
if (strType == "blockindex" && !fRequestShutdown) if (strType == "blockindex" && !fRequestShutdown)
@ -564,6 +572,10 @@ bool CTxDB::LoadBlockIndex()
{ {
break; // if shutdown requested or finished loading block index break; // if shutdown requested or finished loading block index
} }
} // try
catch (std::exception &e) {
return error("%s() : deserialize error", __PRETTY_FUNCTION__);
}
} }
pcursor->close(); pcursor->close();

5
src/db.h

@ -72,8 +72,13 @@ protected:
return false; return false;
// Unserialize value // Unserialize value
try {
CDataStream ssValue((char*)datValue.get_data(), (char*)datValue.get_data() + datValue.get_size(), SER_DISK, CLIENT_VERSION); CDataStream ssValue((char*)datValue.get_data(), (char*)datValue.get_data() + datValue.get_size(), SER_DISK, CLIENT_VERSION);
ssValue >> value; ssValue >> value;
}
catch (std::exception &e) {
return false;
}
// Clear and free memory // Clear and free memory
memset(datValue.get_data(), 0, datValue.get_size()); memset(datValue.get_data(), 0, datValue.get_size());

5
src/main.cpp

@ -2133,8 +2133,9 @@ bool LoadExternalBlockFile(FILE* fileIn)
} }
} }
} }
catch (std::exception &e) catch (std::exception &e) {
{ printf("%s() : Deserialize or I/O error caught during load\n",
__PRETTY_FUNCTION__);
} }
} }
printf("Loaded %i blocks from external file\n", nLoaded); printf("Loaded %i blocks from external file\n", nLoaded);

11
src/main.h

@ -593,7 +593,13 @@ public:
// Read transaction // Read transaction
if (fseek(filein, pos.nTxPos, SEEK_SET) != 0) if (fseek(filein, pos.nTxPos, SEEK_SET) != 0)
return error("CTransaction::ReadFromDisk() : fseek failed"); return error("CTransaction::ReadFromDisk() : fseek failed");
try {
filein >> *this; filein >> *this;
}
catch (std::exception &e) {
return error("%s() : deserialize or I/O error", __PRETTY_FUNCTION__);
}
// Return file pointer // Return file pointer
if (pfileRet) if (pfileRet)
@ -969,7 +975,12 @@ public:
filein.nType |= SER_BLOCKHEADERONLY; filein.nType |= SER_BLOCKHEADERONLY;
// Read block // Read block
try {
filein >> *this; filein >> *this;
}
catch (std::exception &e) {
return error("%s() : deserialize or I/O error", __PRETTY_FUNCTION__);
}
// Check the header // Check the header
if (!CheckProofOfWork(GetHash(), nBits)) if (!CheckProofOfWork(GetHash(), nBits))

Loading…
Cancel
Save