upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBitflash <btf01@proton.me>2026-07-29 17:41:10 -0300
committerGitHub <noreply@github.com>2026-07-29 17:41:10 -0300
commit72683da2b01286030fd61228168712c84aea79d4 (patch)
tree0b299e830282e483a1642e3b2ca38fb68507e646 /src
parentc404e1c7f5d3f04d07e7e5b35a7a8ffcc998d855 (diff)
db: catch BDB exceptions so unreadable files fail cleanly (#34) (#38)
Berkeley DB throws when it cannot open a file written by another platform's build (e.g. BDB 5.3 wallet read by 6.2). Nothing on the call path caught these, so the exception unwound into std::terminate -- surfacing on Windows as STATUS_STACK_BUFFER_OVERRUN (0xC0000409) with no useful message. Three layers of defense: 1. main_gui.cpp: try/catch around LoadBlockIndex() and LoadWallet(), printing the reason and exiting cleanly. 2. db.cpp LoadWallet: try/catch around the record-reading loop (Satoshi's 2009 TODO), tracking the last record type so the error message says what it choked on. 3. db.cpp cleanup paths: every dbenv.close(), pdb->close(), txn_checkpoint() and log_archive() wrapped in try/catch so the environment teardown after a failed open does not throw a second exception into std::terminate. Tested: cross-platform wallet (Linux BDB 5.3 → Windows 6.2) now prints a clear message and exits with code 1, wallet left untouched. Closes #34 Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
Diffstat (limited to 'src')
-rw-r--r--src/db.cpp47
-rw-r--r--src/main_gui.cpp41
2 files changed, 79 insertions, 9 deletions
diff --git a/src/db.cpp b/src/db.cpp
index 199fa44..d43e4ef 100644
--- a/src/db.cpp
+++ b/src/db.cpp
@@ -28,7 +28,8 @@ public:
28 { 28 {
29 if (fDbEnvInit) 29 if (fDbEnvInit)
30 { 30 {
31 dbenv.close(0); 31 try { dbenv.close(0); }
32 catch (...) { }
32 fDbEnvInit = false; 33 fDbEnvInit = false;
33 } 34 }
34 } 35 }
@@ -118,10 +119,10 @@ void CDB::Close()
118 if (!vTxn.empty()) 119 if (!vTxn.empty())
119 vTxn.front()->abort(); 120 vTxn.front()->abort();
120 vTxn.clear(); 121 vTxn.clear();
121 pdb->close(0); 122 try { pdb->close(0); } catch (...) { }
122 delete pdb; 123 delete pdb;
123 pdb = NULL; 124 pdb = NULL;
124 dbenv.txn_checkpoint(0, 0, 0); 125 try { dbenv.txn_checkpoint(0, 0, 0); } catch (...) { }
125 126
126 CRITICAL_BLOCK(cs_db) 127 CRITICAL_BLOCK(cs_db)
127 --mapFileUseCount[strFile]; 128 --mapFileUseCount[strFile];
@@ -154,8 +155,8 @@ void DBFlush(bool fShutdown)
154 { 155 {
155 char** listp; 156 char** listp;
156 if (mapFileUseCount.empty()) 157 if (mapFileUseCount.empty())
157 dbenv.log_archive(&listp, DB_ARCH_REMOVE); 158 try { dbenv.log_archive(&listp, DB_ARCH_REMOVE); } catch (...) { }
158 dbenv.close(0); 159 try { dbenv.close(0); } catch (...) { }
159 fDbEnvInit = false; 160 fDbEnvInit = false;
160 } 161 }
161 } 162 }
@@ -427,7 +428,25 @@ bool CWalletDB::LoadWallet(vector<unsigned char>& vchDefaultKeyRet)
427{ 428{
428 vchDefaultKeyRet.clear(); 429 vchDefaultKeyRet.clear();
429 430
430 //// todo: shouldn't we catch exceptions and try to recover and continue? 431 // Satoshi's "todo: shouldn't we catch exceptions" sat here since 2009, and
432 // it was not a nicety. Nothing on this path caught anything, so a record
433 // this build could not read -- a wallet from another platform's Berkeley
434 // DB, a truncated file, a field it does not understand -- threw out of
435 // LoadWallet, out of main(), into std::terminate and abort(). On Windows
436 // that surfaces as STATUS_STACK_BUFFER_OVERRUN (0xC0000409) in
437 // ucrtbase.dll: no message, no log line past "Loading wallet...", and a
438 // faulting module that has nothing to do with the actual problem.
439 //
440 // The program knew exactly what had gone wrong and threw the reason away.
441 // Now it says so and stops cleanly, which is the difference between "your
442 // wallet is unreadable and here is why" and a crash nobody can act on.
443 //
444 // Deliberately a hard failure rather than skipping the bad record: a
445 // partially loaded wallet is worse than one that refuses to open, because
446 // the balance looks plausible while keys or transactions are missing.
447 string strLastType;
448 try
449 {
431 CRITICAL_BLOCK(cs_mapKeys) 450 CRITICAL_BLOCK(cs_mapKeys)
432 CRITICAL_BLOCK(cs_mapWallet) 451 CRITICAL_BLOCK(cs_mapWallet)
433 { 452 {
@@ -452,6 +471,7 @@ bool CWalletDB::LoadWallet(vector<unsigned char>& vchDefaultKeyRet)
452 // is just the two items serialized one after the other 471 // is just the two items serialized one after the other
453 string strType; 472 string strType;
454 ssKey >> strType; 473 ssKey >> strType;
474 strLastType = strType;
455 if (strType == "name") 475 if (strType == "name")
456 { 476 {
457 string strAddress; 477 string strAddress;
@@ -509,6 +529,21 @@ bool CWalletDB::LoadWallet(vector<unsigned char>& vchDefaultKeyRet)
509 } 529 }
510 } 530 }
511 } 531 }
532 }
533 catch (const std::exception& e)
534 {
535 printf("LoadWallet: wallet.dat could not be read. Failed while handling "
536 "a '%s' record: %s\n", strLastType.c_str(), e.what());
537 printf("LoadWallet: the file was left untouched. A wallet.dat written by "
538 "a different platform's Berkeley DB is the usual cause.\n");
539 return false;
540 }
541 catch (...)
542 {
543 printf("LoadWallet: wallet.dat could not be read. Unknown failure while "
544 "handling a '%s' record.\n", strLastType.c_str());
545 return false;
546 }
512 547
513 printf("nTransactionFee = %lld\n", nTransactionFee); 548 printf("nTransactionFee = %lld\n", nTransactionFee);
514 printf("addrIncoming = %s\n", addrIncoming.ToString().c_str()); 549 printf("addrIncoming = %s\n", addrIncoming.ToString().c_str());
diff --git a/src/main_gui.cpp b/src/main_gui.cpp
index 99e63d4..a94d01d 100644
--- a/src/main_gui.cpp
+++ b/src/main_gui.cpp
@@ -170,11 +170,46 @@ int main(int argc, char* argv[])
170 170
171 ParseStartupArguments(argc, argv); 171 ParseStartupArguments(argc, argv);
172 172
173 printf("Loading block index...\n"); 173 // Berkeley DB reports failure by throwing, and CDB's constructor lets it
174 // through. Nothing on this path caught anything, so an unreadable
175 // blkindex.dat or wallet.dat -- one written by another platform's Berkeley
176 // DB, a truncated file, a version mismatch -- unwound out of main() into
177 // std::terminate and abort(). On Windows that surfaces as
178 // STATUS_STACK_BUFFER_OVERRUN (0xC0000409) inside ucrtbase.dll: no message,
179 // no log line past "Loading wallet...", and a faulting module with nothing
180 // to do with the real problem. Confirmed by stack trace:
181 //
182 // libdb_cxx-6.2.dll -> CDB::CDB(...) -> LoadBlockIndex(...) -> main()
183 //
184 // The program knew what had gone wrong and threw the reason away.
174 string strErrors; 185 string strErrors;
175 if (!LoadBlockIndex()) { fprintf(stderr,"LoadBlockIndex failed\n"); return 1; } 186 printf("Loading block index...\n");
187 try
188 {
189 if (!LoadBlockIndex()) { fprintf(stderr, "LoadBlockIndex failed\n"); return 1; }
190 }
191 catch (const std::exception& e)
192 {
193 fprintf(stderr, "Cannot read the block index: %s\n", e.what());
194 fprintf(stderr, "blkindex.dat and blk0001.dat may be from another machine or "
195 "incomplete. Deleting both is safe -- they are re-downloaded -- "
196 "but never delete wallet.dat, which holds your keys.\n");
197 return 1;
198 }
199
176 printf("Loading wallet...\n"); 200 printf("Loading wallet...\n");
177 if (!LoadWallet()) { fprintf(stderr,"LoadWallet failed\n"); return 1; } 201 try
202 {
203 if (!LoadWallet()) { fprintf(stderr, "LoadWallet failed\n"); return 1; }
204 }
205 catch (const std::exception& e)
206 {
207 fprintf(stderr, "Cannot read wallet.dat: %s\n", e.what());
208 fprintf(stderr, "The file was left untouched. A wallet.dat written by a "
209 "different platform's Berkeley DB is the usual cause; back it "
210 "up before trying anything else.\n");
211 return 1;
212 }
178 printf("Height=%d\n", nBestHeight); 213 printf("Height=%d\n", nBestHeight);
179 ReacceptWalletTransactions(); 214 ReacceptWalletTransactions();
180 215