Skip to content

Commit b1c5991

Browse files
author
MarcoFalke
committed
Merge bitcoin/bitcoin#24812: util/check: Add CHECK_NONFATAL identity function and NONFATAL_UNREACHABLE macro
ee02c8b util/check: Add CHECK_NONFATAL identity function, NONFATAL_UNREACHABLE AND UNREACHABLE macros (Aurèle Oulès) Pull request description: This PR replaces the macro `CHECK_NONFATAL` with an identity function. I simplified the usage of `CHECK_NONFATAL` where applicable in `src/rpc`. This function is useful in sanity checks for RPC and command-line interfaces. Context: bitcoin/bitcoin#24804 (comment). Also adds `UNREACHABLE_NONFATAL` macro. ACKs for top commit: jonatack: ACK ee02c8b MarcoFalke: ACK ee02c8b 🍨 Tree-SHA512: 3cba09223cd7b22e62fe5d0b46c4a024c1d9957d4268ba6d3fb07fcc0a5854fc0886bb3266184e6a7df5df91373b3e84edd6adf6999c4e934aeef8c043b01aa2
2 parents be7a5f2 + ee02c8b commit b1c5991

File tree

7 files changed

+49
-39
lines changed

7 files changed

+49
-39
lines changed

src/rpc/blockchain.cpp

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include <txmempool.h>
3838
#include <undo.h>
3939
#include <univalue.h>
40+
#include <util/check.h>
4041
#include <util/strencodings.h>
4142
#include <util/translation.h>
4243
#include <validation.h>
@@ -785,8 +786,7 @@ static RPCHelpMan pruneblockchain()
785786
}
786787

787788
PruneBlockFilesManual(active_chainstate, height);
788-
const CBlockIndex* block = active_chain.Tip();
789-
CHECK_NONFATAL(block);
789+
const CBlockIndex* block = CHECK_NONFATAL(active_chain.Tip());
790790
while (block->pprev && (block->pprev->nStatus & BLOCK_HAVE_DATA)) {
791791
block = block->pprev;
792792
}
@@ -1200,8 +1200,7 @@ RPCHelpMan getblockchaininfo()
12001200
LOCK(cs_main);
12011201
CChainState& active_chainstate = chainman.ActiveChainstate();
12021202

1203-
const CBlockIndex* tip = active_chainstate.m_chain.Tip();
1204-
CHECK_NONFATAL(tip);
1203+
const CBlockIndex* tip = CHECK_NONFATAL(active_chainstate.m_chain.Tip());
12051204
const int height = tip->nHeight;
12061205
UniValue obj(UniValue::VOBJ);
12071206
obj.pushKV("chain", Params().NetworkIDString());
@@ -1217,8 +1216,7 @@ RPCHelpMan getblockchaininfo()
12171216
obj.pushKV("size_on_disk", chainman.m_blockman.CalculateCurrentUsage());
12181217
obj.pushKV("pruned", node::fPruneMode);
12191218
if (node::fPruneMode) {
1220-
const CBlockIndex* block = tip;
1221-
CHECK_NONFATAL(block);
1219+
const CBlockIndex* block = CHECK_NONFATAL(tip);
12221220
while (block->pprev && (block->pprev->nStatus & BLOCK_HAVE_DATA)) {
12231221
block = block->pprev;
12241222
}
@@ -1309,8 +1307,7 @@ static RPCHelpMan getdeploymentinfo()
13091307

13101308
const CBlockIndex* blockindex;
13111309
if (request.params[0].isNull()) {
1312-
blockindex = active_chainstate.m_chain.Tip();
1313-
CHECK_NONFATAL(blockindex);
1310+
blockindex = CHECK_NONFATAL(active_chainstate.m_chain.Tip());
13141311
} else {
13151312
const uint256 hash(ParseHashV(request.params[0], "blockhash"));
13161313
blockindex = chainman.m_blockman.LookupBlockIndex(hash);
@@ -2131,10 +2128,8 @@ static RPCHelpMan scantxoutset()
21312128
LOCK(cs_main);
21322129
CChainState& active_chainstate = chainman.ActiveChainstate();
21332130
active_chainstate.ForceFlushStateToDisk();
2134-
pcursor = active_chainstate.CoinsDB().Cursor();
2135-
CHECK_NONFATAL(pcursor);
2136-
tip = active_chainstate.m_chain.Tip();
2137-
CHECK_NONFATAL(tip);
2131+
pcursor = CHECK_NONFATAL(active_chainstate.CoinsDB().Cursor());
2132+
tip = CHECK_NONFATAL(active_chainstate.m_chain.Tip());
21382133
}
21392134
bool res = FindScriptPubKey(g_scan_progress, g_should_abort_scan, count, pcursor.get(), needles, coins, node.rpc_interruption_point);
21402135
result.pushKV("success", res);
@@ -2336,8 +2331,7 @@ UniValue CreateUTXOSnapshot(
23362331
}
23372332

23382333
pcursor = chainstate.CoinsDB().Cursor();
2339-
tip = chainstate.m_blockman.LookupBlockIndex(stats.hashBlock);
2340-
CHECK_NONFATAL(tip);
2334+
tip = CHECK_NONFATAL(chainstate.m_blockman.LookupBlockIndex(stats.hashBlock));
23412335
}
23422336

23432337
LOG_TIME_SECONDS(strprintf("writing UTXO snapshot at height %s (%s) to file %s (via %s)",

src/rpc/misc.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -484,9 +484,8 @@ static RPCHelpMan mockscheduler()
484484
throw std::runtime_error("delta_time must be between 1 and 3600 seconds (1 hr)");
485485
}
486486

487-
auto node_context = util::AnyPtr<NodeContext>(request.context);
487+
auto node_context = CHECK_NONFATAL(util::AnyPtr<NodeContext>(request.context));
488488
// protect against null pointer dereference
489-
CHECK_NONFATAL(node_context);
490489
CHECK_NONFATAL(node_context->scheduler);
491490
node_context->scheduler->MockForward(std::chrono::seconds(delta_seconds));
492491

src/rpc/rawtransaction.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include <script/standard.h>
3434
#include <uint256.h>
3535
#include <util/bip32.h>
36+
#include <util/check.h>
3637
#include <util/strencodings.h>
3738
#include <util/string.h>
3839
#include <util/vector.h>
@@ -466,7 +467,7 @@ static RPCHelpMan decodescript()
466467
// Should not be wrapped
467468
return false;
468469
} // no default case, so the compiler can warn about missing cases
469-
CHECK_NONFATAL(false);
470+
NONFATAL_UNREACHABLE();
470471
}()};
471472
if (can_wrap_P2WSH) {
472473
UniValue sr(UniValue::VOBJ);

src/rpc/util.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <script/descriptor.h>
1010
#include <script/signingprovider.h>
1111
#include <tinyformat.h>
12+
#include <util/check.h>
1213
#include <util/strencodings.h>
1314
#include <util/string.h>
1415
#include <util/translation.h>
@@ -542,7 +543,7 @@ RPCHelpMan::RPCHelpMan(std::string name, std::string description, std::vector<RP
542543
// Null values are accepted in all arguments
543544
break;
544545
default:
545-
CHECK_NONFATAL(false);
546+
NONFATAL_UNREACHABLE();
546547
break;
547548
}
548549
}
@@ -793,7 +794,7 @@ void RPCResult::ToSections(Sections& sections, const OuterType outer_type, const
793794
return;
794795
}
795796
case Type::ANY: {
796-
CHECK_NONFATAL(false); // Only for testing
797+
NONFATAL_UNREACHABLE(); // Only for testing
797798
}
798799
case Type::NONE: {
799800
sections.PushSection({indent + "null" + maybe_separator, Description("json null")});
@@ -860,7 +861,7 @@ void RPCResult::ToSections(Sections& sections, const OuterType outer_type, const
860861
return;
861862
}
862863
} // no default case, so the compiler can warn about missing cases
863-
CHECK_NONFATAL(false);
864+
NONFATAL_UNREACHABLE();
864865
}
865866

866867
bool RPCResult::MatchesType(const UniValue& result) const
@@ -938,7 +939,7 @@ bool RPCResult::MatchesType(const UniValue& result) const
938939
return true;
939940
}
940941
} // no default case, so the compiler can warn about missing cases
941-
CHECK_NONFATAL(false);
942+
NONFATAL_UNREACHABLE();
942943
}
943944

944945
void RPCResult::CheckInnerDoc() const
@@ -984,9 +985,9 @@ std::string RPCArg::ToStringObj(const bool oneline) const
984985
case Type::OBJ:
985986
case Type::OBJ_USER_KEYS:
986987
// Currently unused, so avoid writing dead code
987-
CHECK_NONFATAL(false);
988+
NONFATAL_UNREACHABLE();
988989
} // no default case, so the compiler can warn about missing cases
989-
CHECK_NONFATAL(false);
990+
NONFATAL_UNREACHABLE();
990991
}
991992

992993
std::string RPCArg::ToString(const bool oneline) const
@@ -1021,7 +1022,7 @@ std::string RPCArg::ToString(const bool oneline) const
10211022
return "[" + res + "...]";
10221023
}
10231024
} // no default case, so the compiler can warn about missing cases
1024-
CHECK_NONFATAL(false);
1025+
NONFATAL_UNREACHABLE();
10251026
}
10261027

10271028
static std::pair<int64_t, int64_t> ParseRange(const UniValue& value)

src/util/check.h

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,24 @@ class NonFatalCheckError : public std::runtime_error
1818
using std::runtime_error::runtime_error;
1919
};
2020

21+
#define format_internal_error(msg, file, line, func, report) \
22+
strprintf("Internal bug detected: \"%s\"\n%s:%d (%s)\nPlease report this issue here: %s\n", \
23+
msg, file, line, func, report)
24+
25+
/** Helper for CHECK_NONFATAL() */
26+
template <typename T>
27+
T&& inline_check_non_fatal(T&& val, const char* file, int line, const char* func, const char* assertion)
28+
{
29+
if (!(val)) {
30+
throw NonFatalCheckError(
31+
format_internal_error(assertion, file, line, func, PACKAGE_BUGREPORT));
32+
}
33+
34+
return std::forward<T>(val);
35+
}
36+
2137
/**
22-
* Throw a NonFatalCheckError when the condition evaluates to false
38+
* Identity function. Throw a NonFatalCheckError when the condition evaluates to false
2339
*
2440
* This should only be used
2541
* - where the condition is assumed to be true, not for error handling or validating user input
@@ -29,18 +45,8 @@ class NonFatalCheckError : public std::runtime_error
2945
* asserts or recoverable logic errors. A NonFatalCheckError in RPC code is caught and passed as a string to the RPC
3046
* caller, which can then report the issue to the developers.
3147
*/
32-
#define CHECK_NONFATAL(condition) \
33-
do { \
34-
if (!(condition)) { \
35-
throw NonFatalCheckError( \
36-
strprintf("Internal bug detected: '%s'\n" \
37-
"%s:%d (%s)\n" \
38-
"You may report this issue here: %s\n", \
39-
(#condition), \
40-
__FILE__, __LINE__, __func__, \
41-
PACKAGE_BUGREPORT)); \
42-
} \
43-
} while (false)
48+
#define CHECK_NONFATAL(condition) \
49+
inline_check_non_fatal(condition, __FILE__, __LINE__, __func__, #condition)
4450

4551
#if defined(NDEBUG)
4652
#error "Cannot compile without assertions!"
@@ -80,4 +86,13 @@ T&& inline_assertion_check(T&& val, [[maybe_unused]] const char* file, [[maybe_u
8086
*/
8187
#define Assume(val) inline_assertion_check<false>(val, __FILE__, __LINE__, __func__, #val)
8288

89+
/**
90+
* NONFATAL_UNREACHABLE() is a macro that is used to mark unreachable code. It throws a NonFatalCheckError.
91+
* This is used to mark code that is not yet implemented or is not yet reachable.
92+
*/
93+
#define NONFATAL_UNREACHABLE() \
94+
throw NonFatalCheckError( \
95+
format_internal_error("Unreachable code reached (non-fatal)", \
96+
__FILE__, __LINE__, __func__, PACKAGE_BUGREPORT))
97+
8398
#endif // BITCOIN_UTIL_CHECK_H

src/wallet/rpc/backup.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -915,7 +915,7 @@ static std::string RecurseImportData(const CScript& script, ImportData& import_d
915915
case TxoutType::WITNESS_V1_TAPROOT:
916916
return "unrecognized script";
917917
} // no default case, so the compiler can warn about missing cases
918-
CHECK_NONFATAL(false);
918+
NONFATAL_UNREACHABLE();
919919
}
920920

921921
static UniValue ProcessImportLegacy(ImportData& import_data, std::map<CKeyID, CPubKey>& pubkey_map, std::map<CKeyID, CKey>& privkey_map, std::set<CScript>& script_pub_keys, bool& have_solving_data, const UniValue& data, std::vector<CKeyID>& ordered_pubkeys)

test/functional/rpc_misc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def run_test(self):
2727
self.log.info("test CHECK_NONFATAL")
2828
assert_raises_rpc_error(
2929
-1,
30-
'Internal bug detected: \'request.params[9].get_str() != "trigger_internal_bug"\'',
30+
'Internal bug detected: "request.params[9].get_str() != "trigger_internal_bug""',
3131
lambda: node.echo(arg9='trigger_internal_bug'),
3232
)
3333

0 commit comments

Comments
 (0)