Skip to content

Commit 7777f2a

Browse files
author
MarcoFalke
committed
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.
1 parent fa5ceb2 commit 7777f2a

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
@@ -874,7 +874,7 @@ static UniValue getblocktemplate(const JSONRPCRequest& request)
874874
return result;
875875
}
876876

877-
class submitblock_StateCatcher : public CValidationInterface
877+
class submitblock_StateCatcher final : public CValidationInterface
878878
{
879879
public:
880880
uint256 hash;
@@ -942,17 +942,17 @@ static UniValue submitblock(const JSONRPCRequest& request)
942942
}
943943

944944
bool new_block;
945-
submitblock_StateCatcher sc(block.GetHash());
946-
RegisterValidationInterface(&sc);
945+
auto sc = std::make_shared<submitblock_StateCatcher>(block.GetHash());
946+
RegisterSharedValidationInterface(sc);
947947
bool accepted = ProcessNewBlock(Params(), blockptr, /* fForceProcessing */ true, /* fNewBlock */ &new_block);
948-
UnregisterValidationInterface(&sc);
948+
UnregisterSharedValidationInterface(sc);
949949
if (!new_block && accepted) {
950950
return "duplicate";
951951
}
952-
if (!sc.found) {
952+
if (!sc->found) {
953953
return "inconclusive";
954954
}
955-
return BIP22ValidationResult(sc.state);
955+
return BIP22ValidationResult(sc->state);
956956
}
957957

958958
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
@@ -206,10 +206,10 @@ BOOST_AUTO_TEST_CASE(processnewblock_signals_ordering)
206206
}
207207
SyncWithValidationInterfaceQueue();
208208

209-
UnregisterValidationInterface(&sub);
209+
UnregisterSharedValidationInterface(sub);
210210

211211
LOCK(cs_main);
212-
BOOST_CHECK_EQUAL(sub.m_expected_tip, ::ChainActive().Tip()->GetBlockHash());
212+
BOOST_CHECK_EQUAL(sub->m_expected_tip, ::ChainActive().Tip()->GetBlockHash());
213213
}
214214

215215
/**

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)