mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-02 09:46:52 -05:00
util: Add Assert identity function
The utility is primarily useful to dereference pointer types, which are known to be not null at that time. For example, the ArgsManager is known to exist when the wallets are started. Instead of silently relying on that assumption, Assert can be used to abort the program and avoid UB should the assumption ever be violated.
This commit is contained in:
parent
fa457fbd33
commit
fa6ef701ad
4 changed files with 15 additions and 4 deletions
|
@ -11,6 +11,7 @@
|
|||
#include <node/context.h>
|
||||
#include <pow.h>
|
||||
#include <script/standard.h>
|
||||
#include <util/check.h>
|
||||
#include <validation.h>
|
||||
|
||||
CTxIn generatetoaddress(const NodeContext& node, const std::string& address)
|
||||
|
@ -39,9 +40,8 @@ CTxIn MineBlock(const NodeContext& node, const CScript& coinbase_scriptPubKey)
|
|||
|
||||
std::shared_ptr<CBlock> PrepareBlock(const NodeContext& node, const CScript& coinbase_scriptPubKey)
|
||||
{
|
||||
assert(node.mempool);
|
||||
auto block = std::make_shared<CBlock>(
|
||||
BlockAssembler{*node.mempool, Params()}
|
||||
BlockAssembler{*Assert(node.mempool), Params()}
|
||||
.CreateNewBlock(coinbase_scriptPubKey)
|
||||
->block);
|
||||
|
||||
|
|
|
@ -13,6 +13,7 @@
|
|||
#include <random.h>
|
||||
#include <scheduler.h>
|
||||
#include <txmempool.h>
|
||||
#include <util/check.h>
|
||||
#include <util/string.h>
|
||||
|
||||
#include <type_traits>
|
||||
|
|
|
@ -25,7 +25,7 @@ class NonFatalCheckError : public std::runtime_error
|
|||
* - where the condition is assumed to be true, not for error handling or validating user input
|
||||
* - where a failure to fulfill the condition is recoverable and does not abort the program
|
||||
*
|
||||
* For example in RPC code, where it is undersirable to crash the whole program, this can be generally used to replace
|
||||
* For example in RPC code, where it is undesirable to crash the whole program, this can be generally used to replace
|
||||
* asserts or recoverable logic errors. A NonFatalCheckError in RPC code is caught and passed as a string to the RPC
|
||||
* caller, which can then report the issue to the developers.
|
||||
*/
|
||||
|
@ -46,4 +46,14 @@ class NonFatalCheckError : public std::runtime_error
|
|||
#error "Cannot compile without assertions!"
|
||||
#endif
|
||||
|
||||
/** Helper for Assert(). TODO remove in C++14 and replace `decltype(get_pure_r_value(val))` with `T` (templated lambda) */
|
||||
template <typename T>
|
||||
T get_pure_r_value(T&& val)
|
||||
{
|
||||
return std::forward<T>(val);
|
||||
}
|
||||
|
||||
/** Identity function. Abort if the value compares equal to zero */
|
||||
#define Assert(val) [&]() -> decltype(get_pure_r_value(val))& { auto& check = (val); assert(#val && check); return check; }()
|
||||
|
||||
#endif // BITCOIN_UTIL_CHECK_H
|
||||
|
|
|
@ -23,7 +23,7 @@ fi
|
|||
# Macro CHECK_NONFATAL(condition) should be used instead of assert for RPC code, where it
|
||||
# is undesirable to crash the whole program. See: src/util/check.h
|
||||
# src/rpc/server.cpp is excluded from this check since it's mostly meta-code.
|
||||
OUTPUT=$(git grep -nE 'assert *\(.*\);' -- "src/rpc/" "src/wallet/rpc*" ":(exclude)src/rpc/server.cpp")
|
||||
OUTPUT=$(git grep -nE '\<(A|a)ssert *\(.*\);' -- "src/rpc/" "src/wallet/rpc*" ":(exclude)src/rpc/server.cpp")
|
||||
if [[ ${OUTPUT} != "" ]]; then
|
||||
echo "CHECK_NONFATAL(condition) should be used instead of assert for RPC code."
|
||||
echo
|
||||
|
|
Loading…
Add table
Reference in a new issue