Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Prevent crashes due to missing or corrupted database records #1381

Merged
merged 2 commits into from

2 participants

@jgarzik
Owner

Any problems seen during deserialization will throw an uncaught
exception, crashing the entire bitcoin process. Properly return an
error instead, so that we may at least log the error and gracefully
shutdown other portions of the app.

@gavinandresen

ACK, long overdue. Compiled and ran on my Mac.

Jeff Garzik added some commits
Jeff Garzik Prevent crashes due to missing or corrupted database records
Any problems seen during deserialization will throw an uncaught
exception, crashing the entire bitcoin process.  Properly return an
error instead, so that we may at least log the error and gracefully
shutdown other portions of the app.
f94b64c
Jeff Garzik Prevent crashes due to missing or corrupted blk????.dat records
In LoadExternalBlockFile(), errors are already caught... silently.
Add a warning message, even though we do not abort the program due to
load error.
8fe791e
@jgarzik
Owner

Added new commit, catching deser exceptions for CAutoFile (blknnnn.dat) too.

@jgarzik jgarzik merged commit 5d0f7c4 into from
@jgarzik jgarzik deleted the branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 22, 2012
  1. Prevent crashes due to missing or corrupted database records

    Jeff Garzik authored Jeff Garzik committed
    Any problems seen during deserialization will throw an uncaught
    exception, crashing the entire bitcoin process.  Properly return an
    error instead, so that we may at least log the error and gracefully
    shutdown other portions of the app.
  2. Prevent crashes due to missing or corrupted blk????.dat records

    Jeff Garzik authored Jeff Garzik committed
    In LoadExternalBlockFile(), errors are already caught... silently.
    Add a warning message, even though we do not abort the program due to
    load error.
This page is out of date. Refresh to see the latest.
Showing with 37 additions and 8 deletions.
  1. +14 −2 src/db.cpp
  2. +7 −2 src/db.h
  3. +3 −2 src/main.cpp
  4. +13 −2 src/main.h
View
16 src/db.cpp
@@ -416,9 +416,15 @@ bool CTxDB::ReadOwnerTxes(uint160 hash160, int nMinHeight, vector<CTransaction>&
string strType;
uint160 hashItem;
CDiskTxPos pos;
- ssKey >> strType >> hashItem >> pos;
int nItemHeight;
- ssValue >> nItemHeight;
+
+ try {
+ ssKey >> strType >> hashItem >> pos;
+ ssValue >> nItemHeight;
+ }
+ catch (std::exception &e) {
+ return error("%s() : deserialize error", __PRETTY_FUNCTION__);
+ }
// Read transaction
if (strType != "owner" || hashItem != hash160)
@@ -533,6 +539,8 @@ bool CTxDB::LoadBlockIndex()
return false;
// Unserialize
+
+ try {
string strType;
ssKey >> strType;
if (strType == "blockindex" && !fRequestShutdown)
@@ -564,6 +572,10 @@ bool CTxDB::LoadBlockIndex()
{
break; // if shutdown requested or finished loading block index
}
+ } // try
+ catch (std::exception &e) {
+ return error("%s() : deserialize error", __PRETTY_FUNCTION__);
+ }
}
pcursor->close();
View
9 src/db.h
@@ -72,8 +72,13 @@ class CDB
return false;
// Unserialize value
- CDataStream ssValue((char*)datValue.get_data(), (char*)datValue.get_data() + datValue.get_size(), SER_DISK, CLIENT_VERSION);
- ssValue >> value;
+ try {
+ CDataStream ssValue((char*)datValue.get_data(), (char*)datValue.get_data() + datValue.get_size(), SER_DISK, CLIENT_VERSION);
+ ssValue >> value;
+ }
+ catch (std::exception &e) {
+ return false;
+ }
// Clear and free memory
memset(datValue.get_data(), 0, datValue.get_size());
View
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);
View
15 src/main.h
@@ -593,7 +593,13 @@ class CTransaction
// Read transaction
if (fseek(filein, pos.nTxPos, SEEK_SET) != 0)
return error("CTransaction::ReadFromDisk() : fseek failed");
- filein >> *this;
+
+ try {
+ filein >> *this;
+ }
+ catch (std::exception &e) {
+ return error("%s() : deserialize or I/O error", __PRETTY_FUNCTION__);
+ }
// Return file pointer
if (pfileRet)
@@ -969,7 +975,12 @@ class CBlock
filein.nType |= SER_BLOCKHEADERONLY;
// Read block
- filein >> *this;
+ try {
+ filein >> *this;
+ }
+ catch (std::exception &e) {
+ return error("%s() : deserialize or I/O error", __PRETTY_FUNCTION__);
+ }
// Check the header
if (!CheckProofOfWork(GetHash(), nBits))
Something went wrong with that request. Please try again.