Skip to content

Commit f87d0a9

Browse files
author
MarcoFalke
committed
Merge #13534: Don't assert(foo()) where foo() has side effects
6ad0328 Don't assert(foo()) where foo has side effects (practicalswift) Pull request description: Don't `assert(foo())` where `foo` has side effects. From `assert(3)`: > If the macro `NDEBUG` is defined at the moment `<assert.h>` was last included, the macro `assert()` generates no code, and hence does nothing at all. Bitcoin currently cannot be compiled without assertions, but we shouldn't rely on that. Tree-SHA512: 28cff0c6d1c2fb612ca58c9c94142ed01c5cfd0a2fecb8e59cdb6c270374b215d952ed3491d921d84dc1b439fa49da4f0e75e080f6adcbc6b0e08be14e54c170
2 parents afa9600 + 6ad0328 commit f87d0a9

File tree

5 files changed

+14
-8
lines changed

5 files changed

+14
-8
lines changed

src/bench/block_assemble.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ static CTxIn MineBlock(const CScript& coinbase_scriptPubKey)
4141
auto block = PrepareBlock(coinbase_scriptPubKey);
4242

4343
while (!CheckProofOfWork(block->GetHash(), block->nBits, Params().GetConsensus())) {
44-
assert(++block->nNonce);
44+
++block->nNonce;
45+
assert(block->nNonce);
4546
}
4647

4748
bool processed{ProcessNewBlock(Params(), block, true, nullptr)};

src/bench/checkblock.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ static void DeserializeBlockTest(benchmark::State& state)
2828
while (state.KeepRunning()) {
2929
CBlock block;
3030
stream >> block;
31-
assert(stream.Rewind(sizeof(block_bench::block413567)));
31+
bool rewound = stream.Rewind(sizeof(block_bench::block413567));
32+
assert(rewound);
3233
}
3334
}
3435

@@ -45,10 +46,12 @@ static void DeserializeAndCheckBlockTest(benchmark::State& state)
4546
while (state.KeepRunning()) {
4647
CBlock block; // Note that CBlock caches its checked state, so we need to recreate it here
4748
stream >> block;
48-
assert(stream.Rewind(sizeof(block_bench::block413567)));
49+
bool rewound = stream.Rewind(sizeof(block_bench::block413567));
50+
assert(rewound);
4951

5052
CValidationState validationState;
51-
assert(CheckBlock(block, validationState, chainParams->GetConsensus()));
53+
bool checked = CheckBlock(block, validationState, chainParams->GetConsensus());
54+
assert(checked);
5255
}
5356
}
5457

src/httprpc.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,8 +244,9 @@ bool StartHTTPRPC()
244244
// ifdef can be removed once we switch to better endpoint support and API versioning
245245
RegisterHTTPHandler("/wallet/", false, HTTPReq_JSONRPC);
246246
#endif
247-
assert(EventBase());
248-
httpRPCTimerInterface = MakeUnique<HTTPRPCTimerInterface>(EventBase());
247+
struct event_base* eventBase = EventBase();
248+
assert(eventBase);
249+
httpRPCTimerInterface = MakeUnique<HTTPRPCTimerInterface>(eventBase);
249250
RPCSetTimerInterface(httpRPCTimerInterface.get());
250251
return true;
251252
}

src/script/sign.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,8 @@ bool IsSolvable(const SigningProvider& provider, const CScript& script)
469469
static_assert(STANDARD_SCRIPT_VERIFY_FLAGS & SCRIPT_VERIFY_WITNESS_PUBKEYTYPE, "IsSolvable requires standard script flags to include WITNESS_PUBKEYTYPE");
470470
if (ProduceSignature(provider, DUMMY_SIGNATURE_CREATOR, script, sigs)) {
471471
// VerifyScript check is just defensive, and should never fail.
472-
assert(VerifyScript(sigs.scriptSig, script, &sigs.scriptWitness, STANDARD_SCRIPT_VERIFY_FLAGS, DUMMY_CHECKER));
472+
bool verified = VerifyScript(sigs.scriptSig, script, &sigs.scriptWitness, STANDARD_SCRIPT_VERIFY_FLAGS, DUMMY_CHECKER);
473+
assert(verified);
473474
return true;
474475
}
475476
return false;

src/test/txvalidation_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ BOOST_FIXTURE_TEST_CASE(tx_mempool_reject_coinbase, TestChain100Setup)
3030
coinbaseTx.vout[0].nValue = 1 * CENT;
3131
coinbaseTx.vout[0].scriptPubKey = scriptPubKey;
3232

33-
assert(CTransaction(coinbaseTx).IsCoinBase());
33+
BOOST_CHECK(CTransaction(coinbaseTx).IsCoinBase());
3434

3535
CValidationState state;
3636

0 commit comments

Comments
 (0)