From b6862f7b74d0ea7442cf3b9eec7b9556ca47ce4b Mon Sep 17 00:00:00 2001 From: Jeff Garzik Date: Tue, 22 May 2012 15:12:52 -0400 Subject: [PATCH] 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. --- src/db.cpp | 16 ++++++++++++++-- src/db.h | 9 +++++++-- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/db.cpp b/src/db.cpp index 68d3171..d509253 100644 --- a/src/db.cpp +++ b/src/db.cpp @@ -390,9 +390,15 @@ bool CTxDB::ReadOwnerTxes(uint160 hash160, int nMinHeight, vector& 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 8f6c42d..551e093 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()); -- 1.7.1