aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeff Garzik <jgarzik@exmulti.com>2012-05-22 15:12:52 -0400
committerLuke Dashjr <luke-jr+git@utopios.org>2012-05-22 22:47:51 +0000
commitb6862f7b74d0ea7442cf3b9eec7b9556ca47ce4b (patch)
treebcad1d45f93faab126a26e237d98bf65a0fe532b
parent8a39b0d613e6d79b7118eb929b5fe68220584f07 (diff)
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.
-rw-r--r--src/db.cpp16
-rw-r--r--src/db.h9
2 files changed, 21 insertions, 4 deletions
diff --git a/src/db.cpp b/src/db.cpp
index 68d317106d..d509253e4e 100644
--- a/src/db.cpp
+++ b/src/db.cpp
@@ -390,9 +390,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)
@@ -512,6 +518,8 @@ bool CTxDB::LoadBlockIndex()
return false;
// Unserialize
+
+ try {
string strType;
ssKey >> strType;
if (strType == "blockindex")
@@ -543,6 +551,10 @@ bool CTxDB::LoadBlockIndex()
{
break;
}
+ } // try
+ catch (std::exception &e) {
+ return error("%s() : deserialize error", __PRETTY_FUNCTION__);
+ }
}
pcursor->close();
diff --git a/src/db.h b/src/db.h
index 8f6c42d733..551e093443 100644
--- a/src/db.h
+++ b/src/db.h
@@ -72,8 +72,13 @@ protected:
return false;
// Unserialize value
- CDataStream ssValue((char*)datValue.get_data(), (char*)datValue.get_data() + datValue.get_size(), SER_DISK);
- ssValue >> value;
+ try {
+ CDataStream ssValue((char*)datValue.get_data(), (char*)datValue.get_data() + datValue.get_size(), SER_DISK);
+ ssValue >> value;
+ }
+ catch (std::exception &e) {
+ return false;
+ }
// Clear and free memory
memset(datValue.get_data(), 0, datValue.get_size());