Skip to content

Commit faaadda

Browse files
author
MarcoFalke
committed
init: [gui] Avoid UB/crash in InitAndLoadChainstate
1 parent 184159e commit faaadda

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

src/init.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1224,13 +1224,24 @@ static ChainstateLoadResult InitAndLoadChainstate(
12241224
const kernel::CacheSizes& cache_sizes,
12251225
const ArgsManager& args)
12261226
{
1227+
// This function may be called twice, so any dirty state must be reset.
1228+
node.notifications.reset(); // Drop state, such as a cached tip block
1229+
node.mempool.reset();
1230+
node.chainman.reset(); // Drop state, such as an initialized m_block_tree_db
1231+
12271232
const CChainParams& chainparams = Params();
1233+
1234+
Assert(!node.notifications); // Was reset above
1235+
node.notifications = std::make_unique<KernelNotifications>(Assert(node.shutdown_request), node.exit_status, *Assert(node.warnings));
1236+
ReadNotificationArgs(args, *node.notifications);
1237+
12281238
CTxMemPool::Options mempool_opts{
12291239
.check_ratio = chainparams.DefaultConsistencyChecks() ? 1 : 0,
12301240
.signals = node.validation_signals.get(),
12311241
};
12321242
Assert(ApplyArgsManOptions(args, chainparams, mempool_opts)); // no error can happen, already checked in AppInitParameterInteraction
12331243
bilingual_str mempool_error;
1244+
Assert(!node.mempool); // Was reset above
12341245
node.mempool = std::make_unique<CTxMemPool>(mempool_opts, mempool_error);
12351246
if (!mempool_error.empty()) {
12361247
return {ChainstateLoadStatus::FAILURE_FATAL, mempool_error};
@@ -1259,6 +1270,7 @@ static ChainstateLoadResult InitAndLoadChainstate(
12591270
// Creating the chainstate manager internally creates a BlockManager, opens
12601271
// the blocks tree db, and wipes existing block files in case of a reindex.
12611272
// The coinsdb is opened at a later point on LoadChainstate.
1273+
Assert(!node.chainman); // Was reset above
12621274
try {
12631275
node.chainman = std::make_unique<ChainstateManager>(*Assert(node.shutdown_signal), chainman_opts, blockman_opts);
12641276
} catch (dbwrapper_error& e) {
@@ -1696,10 +1708,6 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
16961708

16971709
// ********************************************************* Step 7: load block chain
16981710

1699-
node.notifications = std::make_unique<KernelNotifications>(Assert(node.shutdown_request), node.exit_status, *Assert(node.warnings));
1700-
auto& kernel_notifications{*node.notifications};
1701-
ReadNotificationArgs(args, kernel_notifications);
1702-
17031711
// cache size calculations
17041712
const auto [index_cache_sizes, kernel_cache_sizes] = CalculateCacheSizes(args, g_enabled_filter_types.size());
17051713

@@ -1759,6 +1767,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
17591767
}
17601768

17611769
ChainstateManager& chainman = *Assert(node.chainman);
1770+
auto& kernel_notifications{*Assert(node.notifications)};
17621771

17631772
assert(!node.peerman);
17641773
node.peerman = PeerManager::make(*node.connman, *node.addrman,

0 commit comments

Comments
 (0)