init: do not leave chain DB pointers dangling in the block-index load loop

The load loop deleted pcoinsTip, pcoinsdbview, pcoinscatcher and
pblocktree before reopening them without resetting the pointers. If a
reopen throws (e.g. a corrupt or unopenable blocks/index), the outer
handler reports "Error opening block database", AppInit2() returns false
and Shutdown() then calls FlushStateToDisk() on, and deletes, the
already freed objects: a use-after-free and double delete on the exit
path. Null each pointer right after its delete, as the previous commit
already did for pnotarizations.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-28 10:44:43 -05:00
co-authored by Claude Opus 5.5
parent a7adcbc954
commit 4d7de7a0eb
+7 -6
View File
@@ -2174,12 +2174,13 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
do {
try {
UnloadBlockIndex();
delete pcoinsTip;
delete pcoinsdbview;
delete pcoinscatcher;
delete pblocktree;
delete pnotarizations;
pnotarizations = NULL; // Shutdown() deletes it, so never leave it dangling if a reopen below throws
// Shutdown() deletes (and pcoinsTip: flushes) whatever is non-NULL here, so never leave
// a pointer dangling if one of the reopens below throws.
delete pcoinsTip; pcoinsTip = NULL;
delete pcoinsdbview; pcoinsdbview = NULL;
delete pcoinscatcher; pcoinscatcher = NULL;
delete pblocktree; pblocktree = NULL;
delete pnotarizations; pnotarizations = NULL;
pblocktree = new CBlockTreeDB(nBlockTreeDBCache, false, fReindex, dbCompression, dbMaxOpenFiles);
pcoinsdbview = new CCoinsViewDB(nCoinDBCache, false, fReindex);