mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-04-24 22:09:20 +02:00
Merge #19277: util: Add Assert identity function
fab80fef61refactor: Remove unused EnsureChainman (MarcoFalke)fa34587f1cscripted-diff: Replace EnsureChainman with Assert in unit tests (MarcoFalke)fa6ef701adutil: Add Assert identity function (MarcoFalke)fa457fbd33move-only: Move NDEBUG compile time check to util/check (MarcoFalke) Pull request description: The utility function 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: https://github.com/bitcoin/bitcoin/pull/18923/files#diff-fdb2a1a1d8bc790fcddeb6cf5a42ac55R503 . Instead of silently relying on that assumption, `Assert` can be used to abort the program and avoid UB should the assumption ever be violated. ACKs for top commit: promag: Tested ACKfab80fef61. ryanofsky: Code review ACKfab80fef61Tree-SHA512: 830fba10152ba17d47c4dd42809c7e26f9fe6d38e17a2d5b3f054fd644a5c4c9841286ac421ec9bb28cea9f5faeb659740fcf00de6cc589d423fee7694c42d16
This commit is contained in:
@@ -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.
|
||||
*/
|
||||
@@ -42,4 +42,18 @@ class NonFatalCheckError : public std::runtime_error
|
||||
} \
|
||||
} while (false)
|
||||
|
||||
#if defined(NDEBUG)
|
||||
#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
|
||||
|
||||
Reference in New Issue
Block a user