From 4d7de7a0ebc72ba0d2c8c1cdd632e604a7db350c Mon Sep 17 00:00:00 2001 From: DanS Date: Wed, 28 Oct 2026 10:44:43 -0500 Subject: [PATCH] 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 --- src/init.cpp | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/init.cpp b/src/init.cpp index 2fca612f4..aa23fbbe9 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -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);