Skip to content

Commit 2aed2b3

Browse files
committed
Merge pull request #3344 from gmaxwell/assert_hazards
Sanitize assert usage and refuse to compile with NDEBUG.
2 parents 9ab7a06 + 9b59e3b commit 2aed2b3

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

src/key.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,13 @@ class CECKey {
148148
}
149149

150150
void SetSecretBytes(const unsigned char vch[32]) {
151+
bool ret;
151152
BIGNUM bn;
152153
BN_init(&bn);
153-
assert(BN_bin2bn(vch, 32, &bn));
154-
assert(EC_KEY_regenerate_key(pkey, &bn));
154+
ret = BN_bin2bn(vch, 32, &bn);
155+
assert(ret);
156+
ret = EC_KEY_regenerate_key(pkey, &bn);
157+
assert(ret);
155158
BN_clear_free(&bn);
156159
}
157160

src/main.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
using namespace std;
2828
using namespace boost;
2929

30+
#if defined(NDEBUG)
31+
# error "Bitcoin cannot be compiled without assertions."
32+
#endif
33+
3034
//
3135
// Global state
3236
//
@@ -1266,18 +1270,21 @@ void UpdateTime(CBlockHeader& block, const CBlockIndex* pindexPrev)
12661270

12671271
void UpdateCoins(const CTransaction& tx, CValidationState &state, CCoinsViewCache &inputs, CTxUndo &txundo, int nHeight, const uint256 &txhash)
12681272
{
1273+
bool ret;
12691274
// mark inputs spent
12701275
if (!tx.IsCoinBase()) {
12711276
BOOST_FOREACH(const CTxIn &txin, tx.vin) {
12721277
CCoins &coins = inputs.GetCoins(txin.prevout.hash);
12731278
CTxInUndo undo;
1274-
assert(coins.Spend(txin.prevout, undo));
1279+
ret = coins.Spend(txin.prevout, undo);
1280+
assert(ret);
12751281
txundo.vprevout.push_back(undo);
12761282
}
12771283
}
12781284

12791285
// add outputs
1280-
assert(inputs.SetCoins(txhash, CCoins(tx, nHeight)));
1286+
ret = inputs.SetCoins(txhash, CCoins(tx, nHeight));
1287+
assert(ret);
12811288
}
12821289

12831290
bool CScriptCheck::operator()() const {
@@ -1651,7 +1658,9 @@ bool ConnectBlock(CBlock& block, CValidationState& state, CBlockIndex* pindex, C
16511658
return state.Abort(_("Failed to write transaction index"));
16521659

16531660
// add this block to the view's block chain
1654-
assert(view.SetBestBlock(pindex->GetBlockHash()));
1661+
bool ret;
1662+
ret = view.SetBestBlock(pindex->GetBlockHash());
1663+
assert(ret);
16551664

16561665
// Watch for transactions paying to me
16571666
for (unsigned int i = 0; i < block.vtx.size(); i++)
@@ -1746,7 +1755,9 @@ bool SetBestChain(CValidationState &state, CBlockIndex* pindexNew)
17461755
// Flush changes to global coin state
17471756
int64_t nStart = GetTimeMicros();
17481757
int nModified = view.GetCacheSize();
1749-
assert(view.Flush());
1758+
bool ret;
1759+
ret = view.Flush();
1760+
assert(ret);
17501761
int64_t nTime = GetTimeMicros() - nStart;
17511762
if (fBenchmark)
17521763
LogPrintf("- Flush %i transactions: %.2fms (%.4fms/tx)\n", nModified, 0.001 * nTime, 0.001 * nTime / nModified);

src/wallet.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1298,7 +1298,9 @@ bool CWallet::CreateTransaction(const vector<pair<CScript, int64_t> >& vecSend,
12981298

12991299
// Reserve a new key pair from key pool
13001300
CPubKey vchPubKey;
1301-
assert(reservekey.GetReservedKey(vchPubKey)); // should never fail, as we just unlocked
1301+
bool ret;
1302+
ret = reservekey.GetReservedKey(vchPubKey);
1303+
assert(ret); // should never fail, as we just unlocked
13021304

13031305
scriptChange.SetDestination(vchPubKey.GetID());
13041306
}

0 commit comments

Comments
 (0)