From 8fe791e4e2db4b66f47d44d2ecaf734e2a36e5ed Mon Sep 17 00:00:00 2001
From: Jeff Garzik <jgarzik@exmulti.com>
Date: Tue, 22 May 2012 15:23:17 -0400
Subject: [PATCH] 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.
---
 src/main.cpp |  5 +++--
 src/main.h   | 15 +++++++++++++--
 2 files changed, 16 insertions(+), 4 deletions(-)

diff --git a/src/main.cpp b/src/main.cpp
index b925f8d5d..388947e48 100644
--- a/src/main.cpp
+++ b/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);
diff --git a/src/main.h b/src/main.h
index ac5ba254c..c0fe63a32 100644
--- a/src/main.h
+++ b/src/main.h
@@ -593,7 +593,13 @@ public:
         // 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 @@ public:
             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))