From: Jeff Garzik Date: Tue, 22 May 2012 19:12:52 +0000 (-0400) Subject: Prevent crashes due to missing or corrupted database records X-Git-Tag: v0.4.0-unstable~129^2~6 X-Git-Url: https://git.novaco.in/?p=novacoin.git;a=commitdiff_plain;h=4bd6299efdeb8438d0a58aa7c1083a6faeeaa71b 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. --- diff --git a/src/db.cpp b/src/db.cpp index ef45976..f9151f7 100644 --- a/src/db.cpp +++ b/src/db.cpp @@ -411,9 +411,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) @@ -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(); diff --git a/src/db.h b/src/db.h index 399b62f..dc795d2 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, 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());