Skip to content

Commit cc7d344

Browse files
MarcoFalkefanquake
authored andcommitted
miner: Avoid stack-use-after-return in validationinterface
This is achieved by switching to a shared_ptr. Also, switch the validationinterfaces in the tests to use shared_ptrs for the same reason. Github-Pull: #18742 Rebased-From: 7777f2a
1 parent 37a6207 commit cc7d344

File tree

3 files changed

+14
-16
lines changed

3 files changed

+14
-16
lines changed

src/rpc/mining.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -724,7 +724,7 @@ static UniValue getblocktemplate(const JSONRPCRequest& request)
724724
return result;
725725
}
726726

727-
class submitblock_StateCatcher : public CValidationInterface
727+
class submitblock_StateCatcher final : public CValidationInterface
728728
{
729729
public:
730730
uint256 hash;
@@ -792,17 +792,17 @@ static UniValue submitblock(const JSONRPCRequest& request)
792792
}
793793

794794
bool new_block;
795-
submitblock_StateCatcher sc(block.GetHash());
796-
RegisterValidationInterface(&sc);
795+
auto sc = std::make_shared<submitblock_StateCatcher>(block.GetHash());
796+
RegisterSharedValidationInterface(sc);
797797
bool accepted = ProcessNewBlock(Params(), blockptr, /* fForceProcessing */ true, /* fNewBlock */ &new_block);
798-
UnregisterValidationInterface(&sc);
798+
UnregisterSharedValidationInterface(sc);
799799
if (!new_block && accepted) {
800800
return "duplicate";
801801
}
802-
if (!sc.found) {
802+
if (!sc->found) {
803803
return "inconclusive";
804804
}
805-
return BIP22ValidationResult(sc.state);
805+
return BIP22ValidationResult(sc->state);
806806
}
807807

808808
static UniValue submitheader(const JSONRPCRequest& request)

src/test/validation_block_tests.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ struct MinerTestingSetup : public RegTestingSetup {
3232

3333
BOOST_FIXTURE_TEST_SUITE(validation_block_tests, MinerTestingSetup)
3434

35-
struct TestSubscriber : public CValidationInterface {
35+
struct TestSubscriber final : public CValidationInterface {
3636
uint256 m_expected_tip;
3737

3838
explicit TestSubscriber(uint256 tip) : m_expected_tip(tip) {}
@@ -175,8 +175,8 @@ BOOST_AUTO_TEST_CASE(processnewblock_signals_ordering)
175175
LOCK(cs_main);
176176
initial_tip = ::ChainActive().Tip();
177177
}
178-
TestSubscriber sub(initial_tip->GetBlockHash());
179-
RegisterValidationInterface(&sub);
178+
auto sub = std::make_shared<TestSubscriber>(initial_tip->GetBlockHash());
179+
RegisterSharedValidationInterface(sub);
180180

181181
// create a bunch of threads that repeatedly process a block generated above at random
182182
// this will create parallelism and randomness inside validation - the ValidationInterface
@@ -208,10 +208,10 @@ BOOST_AUTO_TEST_CASE(processnewblock_signals_ordering)
208208
UninterruptibleSleep(std::chrono::milliseconds{100});
209209
}
210210

211-
UnregisterValidationInterface(&sub);
211+
UnregisterSharedValidationInterface(sub);
212212

213213
LOCK(cs_main);
214-
BOOST_CHECK_EQUAL(sub.m_expected_tip, ::ChainActive().Tip()->GetBlockHash());
214+
BOOST_CHECK_EQUAL(sub->m_expected_tip, ::ChainActive().Tip()->GetBlockHash());
215215
}
216216

217217
/**

src/test/validationinterface_tests.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
BOOST_FIXTURE_TEST_SUITE(validationinterface_tests, TestingSetup)
1414

15-
/**
1615
struct TestSubscriberNoop final : public CValidationInterface {
1716
void BlockChecked(const CBlock&, const BlockValidationState&) override {}
1817
};
@@ -34,9 +33,9 @@ BOOST_AUTO_TEST_CASE(unregister_validation_interface_race)
3433
std::thread sub{[&] {
3534
// keep going for about 1 sec, which is 250k iterations
3635
for (int i = 0; i < 250000; i++) {
37-
TestSubscriberNoop sub{};
38-
RegisterValidationInterface(&sub);
39-
UnregisterValidationInterface(&sub);
36+
auto sub = std::make_shared<TestSubscriberNoop>();
37+
RegisterSharedValidationInterface(sub);
38+
UnregisterSharedValidationInterface(sub);
4039
}
4140
// tell the other thread we are done
4241
generate = false;
@@ -46,7 +45,6 @@ BOOST_AUTO_TEST_CASE(unregister_validation_interface_race)
4645
sub.join();
4746
BOOST_CHECK(!generate);
4847
}
49-
*/
5048

5149
class TestInterface : public CValidationInterface
5250
{

0 commit comments

Comments
 (0)