util: Clarify the assertion message in assertion failures (Assert, Assume, etc.) #35461

Manually merged
optout21 merged 3 commits from 2606-assert-msg into master 2026-10-21 08:46:29 +00:00 AGit
optout21 commented 2026-06-04 11:19:53 +00:00 (Migrated from github.com)

Clarify the message in case of assertion failures. Additionally unify the behavior of NONFATAL_UNREACHABLE w.r.t. abort handling.

Motivation: This was discussed in https://github.com/bitcoin/bitcoin/pull/34844#discussion_r3303436384

The error message generated by Assert, Assume, and CHECK_NONFATAL contains the failing assertion condition text, but it's unclear whether that's the failed expectation or the actual case.

For example,
"Internal bug detected: pindex != nullptr"
in fact means that what happened was pindex == nullptr.

Clarify the situation, by inserting "`%s` check failed".

The above example is now:
"Internal bug detected: `pindex != nullptr` check failed"
which is more unambiguous.

Additionally, change NONFATAL_UNREACHABLE to behave like CHECK_NONFATAL, and honor the special abort setting (G_ABORT_ON_FAILED_ASSUME).

Clarify the message in case of assertion failures. Additionally unify the behavior of `NONFATAL_UNREACHABLE` w.r.t. abort handling. Motivation: This was discussed in https://github.com/bitcoin/bitcoin/pull/34844#discussion_r3303436384 The error message generated by `Assert`, `Assume`, and `CHECK_NONFATAL` contains the failing assertion condition text, but it's unclear whether that's the failed expectation or the actual case. For example, `"Internal bug detected: pindex != nullptr"` in fact means that what happened was `pindex == nullptr`. Clarify the situation, by inserting "\`%s\` check failed". The above example is now: ``"Internal bug detected: `pindex != nullptr` check failed"`` which is more unambiguous. Additionally, change `NONFATAL_UNREACHABLE` to behave like `CHECK_NONFATAL`, and honor the special abort setting (`G_ABORT_ON_FAILED_ASSUME`).
DrahtBot commented 2026-06-04 11:19:59 +00:00 (Migrated from github.com)

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

External sites

Reviews

See the guideline and AI policy for information on the review process.

Type Reviewers
ACK l0rinc, maflcko, sedited

If your review is incorrectly listed, please copy-paste <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

Conflicts

No conflicts as of last run.

<!--e57a25ab6845829454e8d69fc972939a--> The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. <!--006a51241073e994b41acfe9ec718e94--> ### External sites * Code coverage & benchmarks: https://corecheck.dev/bitcoin/bitcoin/pulls/35461 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/35461.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/35461#author-ralph <!--021abf342d371248e50ceaed478a90ca--> ### Reviews See [the guideline](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#code-review) and [AI policy](https://github.com/bitcoin/bitcoin/blob/master/doc/AI_POLICY.md) for information on the review process. | Type | Reviewers | | ---- | --------- | | ACK | [l0rinc](https://github.com/bitcoin/bitcoin/pull/35461#issuecomment-6300652651), [maflcko](https://github.com/bitcoin/bitcoin/pull/35461#issuecomment-6310899524), [sedited](https://github.com/bitcoin/bitcoin/pull/35461#pullrequestreview-5597008452) | If your review is incorrectly listed, please copy-paste <code>&lt;!--meta-tag:bot-skip--&gt;</code> into the comment that the bot should ignore. <!--174a7506f384e20aa4161008e828411d--> ### Conflicts No conflicts as of last run. <!--5faf32d7da4f0f540f40219e4f7537a3-->
optout21 commented 2026-06-04 12:33:41 +00:00 (Migrated from github.com)

Added a test update (following CI failures), condensed util_check test.

Added a test update (following CI failures), condensed util_check test.
l0rinc (Migrated from github.com) approved these changes 2026-06-04 19:45:29 +00:00
l0rinc (Migrated from github.com) left a comment

Concept ACK, it's a tiny difference that we don't often expect to encounter, but when we do, it better tell us a realistic story.

Left a few nits about slightly clearer phrasing and leaving the new formatter private.

Local patch
diff --git a/src/test/util_check_tests.cpp b/src/test/util_check_tests.cpp
index 3a47ecb3c2..8a92376608 100644
--- a/src/test/util_check_tests.cpp
+++ b/src/test/util_check_tests.cpp
@@ -9,6 +9,13 @@
 
 BOOST_AUTO_TEST_SUITE(util_check_tests)
 
+namespace {
+void TriggerNonFatalUnreachable()
+{
+    NONFATAL_UNREACHABLE();
+}
+} // namespace
+
 BOOST_AUTO_TEST_CASE(check_pass)
 {
     Assume(true);
@@ -22,25 +29,21 @@ BOOST_AUTO_TEST_CASE(check_fail)
     test_only_CheckFailuresAreExceptionsNotAborts mock_checks{};
 
     if constexpr (G_ABORT_ON_FAILED_ASSUME) {
-        BOOST_CHECK_EXCEPTION(Assume(false), NonFatalCheckError, HasReason{"Internal bug detected: Failed: 'false'"});
+        BOOST_CHECK_EXCEPTION(Assume(false), NonFatalCheckError, HasReason{"Internal bug detected: `false` check failed"});
     } else {
         BOOST_CHECK_NO_THROW(Assume(false));
     }
-    BOOST_CHECK_EXCEPTION(Assert(false), NonFatalCheckError, HasReason{"Internal bug detected: Failed: 'false'"});
-    BOOST_CHECK_EXCEPTION(CHECK_NONFATAL(false), NonFatalCheckError, HasReason{"Internal bug detected: Failed: 'false'"});
+    BOOST_CHECK_EXCEPTION(Assert(false), NonFatalCheckError, HasReason{"Internal bug detected: `false` check failed"});
+    BOOST_CHECK_EXCEPTION(CHECK_NONFATAL(false), NonFatalCheckError, HasReason{"Internal bug detected: `false` check failed"});
 
     // Repeat with a more realistic assert condition
     void* this_should_be_nonnull{nullptr};
-    BOOST_CHECK_EXCEPTION(Assert(this_should_be_nonnull != nullptr), NonFatalCheckError, HasReason{"Internal bug detected: Failed: 'this_should_be_nonnull != nullptr'"});
+    BOOST_CHECK_EXCEPTION(Assert(this_should_be_nonnull != nullptr), NonFatalCheckError, HasReason{"Internal bug detected: `this_should_be_nonnull != nullptr` check failed"});
 }
 
-BOOST_AUTO_TEST_CASE(check_exception_constructor)
+BOOST_AUTO_TEST_CASE(unreachable_diagnostic_unchanged)
 {
-    const auto assert_text{"pointer != nullptr"};
-    const auto location{std::source_location::current()};
-
-    BOOST_CHECK(std::string_view{NonFatalCheckError("some_message", location).what()}.find("Internal bug detected: some_message\n") != std::string_view::npos);
-    BOOST_CHECK(std::string_view{NonFatalCheckError::CreateFailedAssert(assert_text, location).what()}.find("Internal bug detected: Failed: 'pointer != nullptr'\n") != std::string_view::npos);
+    BOOST_CHECK_EXCEPTION(TriggerNonFatalUnreachable(), NonFatalCheckError, HasReason{"Internal bug detected: Unreachable code reached (non-fatal)"});
 }
 
 BOOST_AUTO_TEST_SUITE_END()
diff --git a/src/util/check.cpp b/src/util/check.cpp
index 36885d6db1..f67fb5374f 100644
--- a/src/util/check.cpp
+++ b/src/util/check.cpp
@@ -15,6 +15,11 @@
 #include <string>
 #include <string_view>
 
+std::string StrFormatFailedCheck(std::string_view assertion)
+{
+    return strprintf("`%s` check failed", assertion);
+}
+
 std::string StrFormatInternalBug(std::string_view msg, const std::source_location& loc)
 {
     return strprintf("Internal bug detected: %s\n%s:%d (%s)\n"
@@ -29,19 +34,14 @@ NonFatalCheckError::NonFatalCheckError(std::string_view msg, const std::source_l
 {
 }
 
-NonFatalCheckError NonFatalCheckError::CreateFailedAssert(std::string_view assertion, const std::source_location& loc)
-{
-    return NonFatalCheckError(strprintf("Failed: '%s'", assertion), loc);
-}
-
 bool g_detail_test_only_CheckFailuresAreExceptionsNotAborts{false};
 
 void assertion_fail(const std::source_location& loc, std::string_view assertion)
 {
     if (g_detail_test_only_CheckFailuresAreExceptionsNotAborts) {
-        throw NonFatalCheckError::CreateFailedAssert(assertion, loc);
+        throw NonFatalCheckError{StrFormatFailedCheck(assertion), loc};
     }
-    auto str = strprintf("%s:%s %s: Assertion `%s' failed.\n", loc.file_name(), loc.line(), loc.function_name(), assertion);
+    auto str = strprintf("%s:%s %s: Assertion failed: %s.\n", loc.file_name(), loc.line(), loc.function_name(), StrFormatFailedCheck(assertion));
     fwrite(str.data(), 1, str.size(), stderr);
     std::abort();
 }
diff --git a/src/util/check.h b/src/util/check.h
index 4a7a52b6a8..3ffe8af282 100644
--- a/src/util/check.h
+++ b/src/util/check.h
@@ -57,13 +57,12 @@ struct test_only_CheckFailuresAreExceptionsNotAborts {
 };
 
 std::string StrFormatInternalBug(std::string_view msg, const std::source_location& loc);
+std::string StrFormatFailedCheck(std::string_view assertion);
 
 class NonFatalCheckError : public std::runtime_error
 {
 public:
     NonFatalCheckError(std::string_view msg, const std::source_location& loc);
-    /// Create an instance in case of a failed assert condition, message is constructed accordingly
-    static NonFatalCheckError CreateFailedAssert(std::string_view assertion, const std::source_location& loc);
 };
 
 /** Internal helper */
@@ -77,7 +76,7 @@ T&& inline_check_non_fatal(LIFETIMEBOUND T&& val, const std::source_location& lo
         if constexpr (G_ABORT_ON_FAILED_ASSUME) {
             assertion_fail(loc, assertion);
         }
-        throw NonFatalCheckError::CreateFailedAssert(assertion, loc);
+        throw NonFatalCheckError{StrFormatFailedCheck(assertion), loc};
     }
     return std::forward<T>(val);
 }
diff --git a/test/functional/rpc_misc.py b/test/functional/rpc_misc.py
index 412c9455ce..7fd104a815 100755
--- a/test/functional/rpc_misc.py
+++ b/test/functional/rpc_misc.py
@@ -27,6 +27,7 @@ class RpcMiscTest(BitcoinTestFramework):
 
         self.log.info("test CHECK_NONFATAL")
         msg_internal_bug = 'request.params[9].get_str() != "trigger_internal_bug"'
+        msg_failed_check = f"`{msg_internal_bug}` check failed"
         self.restart_node(0)  # Required to flush the chainstate
         try:
             node.echo(arg9="trigger_internal_bug")
@@ -41,7 +42,7 @@ class RpcMiscTest(BitcoinTestFramework):
             self.start_node(0)
         except JSONRPCException as e:
             assert_equal(e.error["code"], -1)
-            assert f"Internal bug detected: Failed: '{msg_internal_bug}'" in e.error["message"]
+            assert f"Internal bug detected: {msg_failed_check}" in e.error["message"]
 
         self.log.info("test max arg size")
         ARG_SZ_COMMON = 131071  # Common limit, used previously in the test framework, serves as a regression test
Concept ACK, it's a tiny difference that we don't often expect to encounter, but when we do, it better tell us a realistic story. Left a few nits about slightly clearer phrasing and leaving the new formatter private. <details><summary>Local patch</summary> ```patch diff --git a/src/test/util_check_tests.cpp b/src/test/util_check_tests.cpp index 3a47ecb3c2..8a92376608 100644 --- a/src/test/util_check_tests.cpp +++ b/src/test/util_check_tests.cpp @@ -9,6 +9,13 @@ BOOST_AUTO_TEST_SUITE(util_check_tests) +namespace { +void TriggerNonFatalUnreachable() +{ + NONFATAL_UNREACHABLE(); +} +} // namespace + BOOST_AUTO_TEST_CASE(check_pass) { Assume(true); @@ -22,25 +29,21 @@ BOOST_AUTO_TEST_CASE(check_fail) test_only_CheckFailuresAreExceptionsNotAborts mock_checks{}; if constexpr (G_ABORT_ON_FAILED_ASSUME) { - BOOST_CHECK_EXCEPTION(Assume(false), NonFatalCheckError, HasReason{"Internal bug detected: Failed: 'false'"}); + BOOST_CHECK_EXCEPTION(Assume(false), NonFatalCheckError, HasReason{"Internal bug detected: `false` check failed"}); } else { BOOST_CHECK_NO_THROW(Assume(false)); } - BOOST_CHECK_EXCEPTION(Assert(false), NonFatalCheckError, HasReason{"Internal bug detected: Failed: 'false'"}); - BOOST_CHECK_EXCEPTION(CHECK_NONFATAL(false), NonFatalCheckError, HasReason{"Internal bug detected: Failed: 'false'"}); + BOOST_CHECK_EXCEPTION(Assert(false), NonFatalCheckError, HasReason{"Internal bug detected: `false` check failed"}); + BOOST_CHECK_EXCEPTION(CHECK_NONFATAL(false), NonFatalCheckError, HasReason{"Internal bug detected: `false` check failed"}); // Repeat with a more realistic assert condition void* this_should_be_nonnull{nullptr}; - BOOST_CHECK_EXCEPTION(Assert(this_should_be_nonnull != nullptr), NonFatalCheckError, HasReason{"Internal bug detected: Failed: 'this_should_be_nonnull != nullptr'"}); + BOOST_CHECK_EXCEPTION(Assert(this_should_be_nonnull != nullptr), NonFatalCheckError, HasReason{"Internal bug detected: `this_should_be_nonnull != nullptr` check failed"}); } -BOOST_AUTO_TEST_CASE(check_exception_constructor) +BOOST_AUTO_TEST_CASE(unreachable_diagnostic_unchanged) { - const auto assert_text{"pointer != nullptr"}; - const auto location{std::source_location::current()}; - - BOOST_CHECK(std::string_view{NonFatalCheckError("some_message", location).what()}.find("Internal bug detected: some_message\n") != std::string_view::npos); - BOOST_CHECK(std::string_view{NonFatalCheckError::CreateFailedAssert(assert_text, location).what()}.find("Internal bug detected: Failed: 'pointer != nullptr'\n") != std::string_view::npos); + BOOST_CHECK_EXCEPTION(TriggerNonFatalUnreachable(), NonFatalCheckError, HasReason{"Internal bug detected: Unreachable code reached (non-fatal)"}); } BOOST_AUTO_TEST_SUITE_END() diff --git a/src/util/check.cpp b/src/util/check.cpp index 36885d6db1..f67fb5374f 100644 --- a/src/util/check.cpp +++ b/src/util/check.cpp @@ -15,6 +15,11 @@ #include <string> #include <string_view> +std::string StrFormatFailedCheck(std::string_view assertion) +{ + return strprintf("`%s` check failed", assertion); +} + std::string StrFormatInternalBug(std::string_view msg, const std::source_location& loc) { return strprintf("Internal bug detected: %s\n%s:%d (%s)\n" @@ -29,19 +34,14 @@ NonFatalCheckError::NonFatalCheckError(std::string_view msg, const std::source_l { } -NonFatalCheckError NonFatalCheckError::CreateFailedAssert(std::string_view assertion, const std::source_location& loc) -{ - return NonFatalCheckError(strprintf("Failed: '%s'", assertion), loc); -} - bool g_detail_test_only_CheckFailuresAreExceptionsNotAborts{false}; void assertion_fail(const std::source_location& loc, std::string_view assertion) { if (g_detail_test_only_CheckFailuresAreExceptionsNotAborts) { - throw NonFatalCheckError::CreateFailedAssert(assertion, loc); + throw NonFatalCheckError{StrFormatFailedCheck(assertion), loc}; } - auto str = strprintf("%s:%s %s: Assertion `%s' failed.\n", loc.file_name(), loc.line(), loc.function_name(), assertion); + auto str = strprintf("%s:%s %s: Assertion failed: %s.\n", loc.file_name(), loc.line(), loc.function_name(), StrFormatFailedCheck(assertion)); fwrite(str.data(), 1, str.size(), stderr); std::abort(); } diff --git a/src/util/check.h b/src/util/check.h index 4a7a52b6a8..3ffe8af282 100644 --- a/src/util/check.h +++ b/src/util/check.h @@ -57,13 +57,12 @@ struct test_only_CheckFailuresAreExceptionsNotAborts { }; std::string StrFormatInternalBug(std::string_view msg, const std::source_location& loc); +std::string StrFormatFailedCheck(std::string_view assertion); class NonFatalCheckError : public std::runtime_error { public: NonFatalCheckError(std::string_view msg, const std::source_location& loc); - /// Create an instance in case of a failed assert condition, message is constructed accordingly - static NonFatalCheckError CreateFailedAssert(std::string_view assertion, const std::source_location& loc); }; /** Internal helper */ @@ -77,7 +76,7 @@ T&& inline_check_non_fatal(LIFETIMEBOUND T&& val, const std::source_location& lo if constexpr (G_ABORT_ON_FAILED_ASSUME) { assertion_fail(loc, assertion); } - throw NonFatalCheckError::CreateFailedAssert(assertion, loc); + throw NonFatalCheckError{StrFormatFailedCheck(assertion), loc}; } return std::forward<T>(val); } diff --git a/test/functional/rpc_misc.py b/test/functional/rpc_misc.py index 412c9455ce..7fd104a815 100755 --- a/test/functional/rpc_misc.py +++ b/test/functional/rpc_misc.py @@ -27,6 +27,7 @@ class RpcMiscTest(BitcoinTestFramework): self.log.info("test CHECK_NONFATAL") msg_internal_bug = 'request.params[9].get_str() != "trigger_internal_bug"' + msg_failed_check = f"`{msg_internal_bug}` check failed" self.restart_node(0) # Required to flush the chainstate try: node.echo(arg9="trigger_internal_bug") @@ -41,7 +42,7 @@ class RpcMiscTest(BitcoinTestFramework): self.start_node(0) except JSONRPCException as e: assert_equal(e.error["code"], -1) - assert f"Internal bug detected: Failed: '{msg_internal_bug}'" in e.error["message"] + assert f"Internal bug detected: {msg_failed_check}" in e.error["message"] self.log.info("test max arg size") ARG_SZ_COMMON = 131071 # Common limit, used previously in the test framework, serves as a regression test ``` </details>
l0rinc (Migrated from github.com) commented 2026-06-04 19:36:29 +00:00

I don't particularly like the double colon and starting with capital letters in the middle, please consider avoiding the two stacked labels by adding the text after the failure, e.g:

diff --git a/src/test/util_check_tests.cpp b/src/test/util_check_tests.cpp
index 49954dff9c..0d944427f8 100644
--- a/src/test/util_check_tests.cpp
+++ b/src/test/util_check_tests.cpp
@@ -22,16 +22,16 @@ BOOST_AUTO_TEST_CASE(check_fail)
     test_only_CheckFailuresAreExceptionsNotAborts mock_checks{};
 
     if constexpr (G_ABORT_ON_FAILED_ASSUME) {
-        BOOST_CHECK_EXCEPTION(Assume(false), NonFatalCheckError, HasReason{"Internal bug detected: Failed: 'false'"});
+        BOOST_CHECK_EXCEPTION(Assume(false), NonFatalCheckError, HasReason{"Internal bug detected: `false` check failed"});
     } else {
         BOOST_CHECK_NO_THROW(Assume(false));
     }
-    BOOST_CHECK_EXCEPTION(Assert(false), NonFatalCheckError, HasReason{"Internal bug detected: Failed: 'false'"});
-    BOOST_CHECK_EXCEPTION(CHECK_NONFATAL(false), NonFatalCheckError, HasReason{"Internal bug detected: Failed: 'false'"});
+    BOOST_CHECK_EXCEPTION(Assert(false), NonFatalCheckError, HasReason{"Internal bug detected: `false` check failed"});
+    BOOST_CHECK_EXCEPTION(CHECK_NONFATAL(false), NonFatalCheckError, HasReason{"Internal bug detected: `false` check failed"});
 
     // Repeat with a more realistic assert condition
     void* this_should_be_nonnull{nullptr};
-    BOOST_CHECK_EXCEPTION(Assert(this_should_be_nonnull != nullptr), NonFatalCheckError, HasReason{"Internal bug detected: Failed: 'this_should_be_nonnull != nullptr'"});
+    BOOST_CHECK_EXCEPTION(Assert(this_should_be_nonnull != nullptr), NonFatalCheckError, HasReason{"Internal bug detected: `this_should_be_nonnull != nullptr` check failed"});
 }
 
 BOOST_AUTO_TEST_SUITE_END()
diff --git a/src/util/check.cpp b/src/util/check.cpp
index e26f3d7b41..b62520b1b4 100644
--- a/src/util/check.cpp
+++ b/src/util/check.cpp
@@ -17,7 +17,7 @@
 
 std::string StrFormatFailedCheck(std::string_view assertion)
 {
-    return strprintf("Failed: '%s'", assertion);
+    return strprintf("`%s` check failed", assertion);
 }
 
 std::string StrFormatInternalBug(std::string_view msg, const std::source_location& loc)
diff --git a/test/functional/rpc_misc.py b/test/functional/rpc_misc.py
index 412c9455ce..7fd104a815 100755
--- a/test/functional/rpc_misc.py
+++ b/test/functional/rpc_misc.py
@@ -27,6 +27,7 @@ class RpcMiscTest(BitcoinTestFramework):
 
         self.log.info("test CHECK_NONFATAL")
         msg_internal_bug = 'request.params[9].get_str() != "trigger_internal_bug"'
+        msg_failed_check = f"`{msg_internal_bug}` check failed"
         self.restart_node(0)  # Required to flush the chainstate
         try:
             node.echo(arg9="trigger_internal_bug")
@@ -41,7 +42,7 @@ class RpcMiscTest(BitcoinTestFramework):
             self.start_node(0)
         except JSONRPCException as e:
             assert_equal(e.error["code"], -1)
-            assert f"Internal bug detected: Failed: '{msg_internal_bug}'" in e.error["message"]
+            assert f"Internal bug detected: {msg_failed_check}" in e.error["message"]
 
         self.log.info("test max arg size")
         ARG_SZ_COMMON = 131071  # Common limit, used previously in the test framework, serves as a regression test
I don't particularly like the double colon and starting with capital letters in the middle, please consider avoiding the two stacked labels by adding the text after the failure, e.g: ```patch diff --git a/src/test/util_check_tests.cpp b/src/test/util_check_tests.cpp index 49954dff9c..0d944427f8 100644 --- a/src/test/util_check_tests.cpp +++ b/src/test/util_check_tests.cpp @@ -22,16 +22,16 @@ BOOST_AUTO_TEST_CASE(check_fail) test_only_CheckFailuresAreExceptionsNotAborts mock_checks{}; if constexpr (G_ABORT_ON_FAILED_ASSUME) { - BOOST_CHECK_EXCEPTION(Assume(false), NonFatalCheckError, HasReason{"Internal bug detected: Failed: 'false'"}); + BOOST_CHECK_EXCEPTION(Assume(false), NonFatalCheckError, HasReason{"Internal bug detected: `false` check failed"}); } else { BOOST_CHECK_NO_THROW(Assume(false)); } - BOOST_CHECK_EXCEPTION(Assert(false), NonFatalCheckError, HasReason{"Internal bug detected: Failed: 'false'"}); - BOOST_CHECK_EXCEPTION(CHECK_NONFATAL(false), NonFatalCheckError, HasReason{"Internal bug detected: Failed: 'false'"}); + BOOST_CHECK_EXCEPTION(Assert(false), NonFatalCheckError, HasReason{"Internal bug detected: `false` check failed"}); + BOOST_CHECK_EXCEPTION(CHECK_NONFATAL(false), NonFatalCheckError, HasReason{"Internal bug detected: `false` check failed"}); // Repeat with a more realistic assert condition void* this_should_be_nonnull{nullptr}; - BOOST_CHECK_EXCEPTION(Assert(this_should_be_nonnull != nullptr), NonFatalCheckError, HasReason{"Internal bug detected: Failed: 'this_should_be_nonnull != nullptr'"}); + BOOST_CHECK_EXCEPTION(Assert(this_should_be_nonnull != nullptr), NonFatalCheckError, HasReason{"Internal bug detected: `this_should_be_nonnull != nullptr` check failed"}); } BOOST_AUTO_TEST_SUITE_END() diff --git a/src/util/check.cpp b/src/util/check.cpp index e26f3d7b41..b62520b1b4 100644 --- a/src/util/check.cpp +++ b/src/util/check.cpp @@ -17,7 +17,7 @@ std::string StrFormatFailedCheck(std::string_view assertion) { - return strprintf("Failed: '%s'", assertion); + return strprintf("`%s` check failed", assertion); } std::string StrFormatInternalBug(std::string_view msg, const std::source_location& loc) diff --git a/test/functional/rpc_misc.py b/test/functional/rpc_misc.py index 412c9455ce..7fd104a815 100755 --- a/test/functional/rpc_misc.py +++ b/test/functional/rpc_misc.py @@ -27,6 +27,7 @@ class RpcMiscTest(BitcoinTestFramework): self.log.info("test CHECK_NONFATAL") msg_internal_bug = 'request.params[9].get_str() != "trigger_internal_bug"' + msg_failed_check = f"`{msg_internal_bug}` check failed" self.restart_node(0) # Required to flush the chainstate try: node.echo(arg9="trigger_internal_bug") @@ -41,7 +42,7 @@ class RpcMiscTest(BitcoinTestFramework): self.start_node(0) except JSONRPCException as e: assert_equal(e.error["code"], -1) - assert f"Internal bug detected: Failed: '{msg_internal_bug}'" in e.error["message"] + assert f"Internal bug detected: {msg_failed_check}" in e.error["message"] self.log.info("test max arg size") ARG_SZ_COMMON = 131071 # Common limit, used previously in the test framework, serves as a regression test ```
l0rinc (Migrated from github.com) commented 2026-06-04 19:41:54 +00:00

Could this test the note about non-assert errors directly? Checking NONFATAL_UNREACHABLE keeps the coverage tied to the behavior reviewers need to preserve, instead of only exercising the constructor plumbing.

diff --git a/src/test/util_check_tests.cpp b/src/test/util_check_tests.cpp
--- a/src/test/util_check_tests.cpp	(revision 4ffb91e3bbf0a06166b2a7bbd7f543d2f5c6b36d)
+++ b/src/test/util_check_tests.cpp	(revision 014fae1a7202498a6e9022a1f4215b0acf23cc9c)
@@ -9,6 +9,13 @@
 
 BOOST_AUTO_TEST_SUITE(util_check_tests)
 
+namespace {
+void TriggerNonFatalUnreachable()
+{
+    NONFATAL_UNREACHABLE();
+}
+} // namespace
+
 BOOST_AUTO_TEST_CASE(check_pass)
 {
     Assume(true);
@@ -34,4 +41,9 @@
     BOOST_CHECK_EXCEPTION(Assert(this_should_be_nonnull != nullptr), NonFatalCheckError, HasReason{"Internal bug detected: `this_should_be_nonnull != nullptr` check failed"});
 }
 
+BOOST_AUTO_TEST_CASE(unreachable_diagnostic_unchanged)
+{
+    BOOST_CHECK_EXCEPTION(TriggerNonFatalUnreachable(), NonFatalCheckError, HasReason{"Internal bug detected: Unreachable code reached (non-fatal)"});
+}
+
 BOOST_AUTO_TEST_SUITE_END()
Could this test the note about non-assert errors directly? Checking NONFATAL_UNREACHABLE keeps the coverage tied to the behavior reviewers need to preserve, instead of only exercising the constructor plumbing. ```patch diff --git a/src/test/util_check_tests.cpp b/src/test/util_check_tests.cpp --- a/src/test/util_check_tests.cpp (revision 4ffb91e3bbf0a06166b2a7bbd7f543d2f5c6b36d) +++ b/src/test/util_check_tests.cpp (revision 014fae1a7202498a6e9022a1f4215b0acf23cc9c) @@ -9,6 +9,13 @@ BOOST_AUTO_TEST_SUITE(util_check_tests) +namespace { +void TriggerNonFatalUnreachable() +{ + NONFATAL_UNREACHABLE(); +} +} // namespace + BOOST_AUTO_TEST_CASE(check_pass) { Assume(true); @@ -34,4 +41,9 @@ BOOST_CHECK_EXCEPTION(Assert(this_should_be_nonnull != nullptr), NonFatalCheckError, HasReason{"Internal bug detected: `this_should_be_nonnull != nullptr` check failed"}); } +BOOST_AUTO_TEST_CASE(unreachable_diagnostic_unchanged) +{ + BOOST_CHECK_EXCEPTION(TriggerNonFatalUnreachable(), NonFatalCheckError, HasReason{"Internal bug detected: Unreachable code reached (non-fatal)"}); +} + BOOST_AUTO_TEST_SUITE_END() ```
l0rinc (Migrated from github.com) commented 2026-06-04 19:38:19 +00:00

Could we avoid adding a public NonFatalCheckError factory? The new formatting is only used by Assert, Assume, and CHECK_NONFATAL, so a helper for the formatted string keeps the generic exception constructor as the only public error API:

diff --git a/src/test/util_check_tests.cpp b/src/test/util_check_tests.cpp
index 3a47ecb3c2..49954dff9c 100644
--- a/src/test/util_check_tests.cpp
+++ b/src/test/util_check_tests.cpp
@@ -34,13 +34,4 @@ BOOST_AUTO_TEST_CASE(check_fail)
     BOOST_CHECK_EXCEPTION(Assert(this_should_be_nonnull != nullptr), NonFatalCheckError, HasReason{"Internal bug detected: Failed: 'this_should_be_nonnull != nullptr'"});
 }
 
-BOOST_AUTO_TEST_CASE(check_exception_constructor)
-{
-    const auto assert_text{"pointer != nullptr"};
-    const auto location{std::source_location::current()};
-
-    BOOST_CHECK(std::string_view{NonFatalCheckError("some_message", location).what()}.find("Internal bug detected: some_message\n") != std::string_view::npos);
-    BOOST_CHECK(std::string_view{NonFatalCheckError::CreateFailedAssert(assert_text, location).what()}.find("Internal bug detected: Failed: 'pointer != nullptr'\n") != std::string_view::npos);
-}
-
 BOOST_AUTO_TEST_SUITE_END()
diff --git a/src/util/check.cpp b/src/util/check.cpp
index 36885d6db1..e26f3d7b41 100644
--- a/src/util/check.cpp
+++ b/src/util/check.cpp
@@ -15,6 +15,11 @@
 #include <string>
 #include <string_view>
 
+std::string StrFormatFailedCheck(std::string_view assertion)
+{
+    return strprintf("Failed: '%s'", assertion);
+}
+
 std::string StrFormatInternalBug(std::string_view msg, const std::source_location& loc)
 {
     return strprintf("Internal bug detected: %s\n%s:%d (%s)\n"
@@ -29,17 +34,12 @@ NonFatalCheckError::NonFatalCheckError(std::string_view msg, const std::source_l
 {
 }
 
-NonFatalCheckError NonFatalCheckError::CreateFailedAssert(std::string_view assertion, const std::source_location& loc)
-{
-    return NonFatalCheckError(strprintf("Failed: '%s'", assertion), loc);
-}
-
 bool g_detail_test_only_CheckFailuresAreExceptionsNotAborts{false};
 
 void assertion_fail(const std::source_location& loc, std::string_view assertion)
 {
     if (g_detail_test_only_CheckFailuresAreExceptionsNotAborts) {
-        throw NonFatalCheckError::CreateFailedAssert(assertion, loc);
+        throw NonFatalCheckError{StrFormatFailedCheck(assertion), loc};
     }
     auto str = strprintf("%s:%s %s: Assertion `%s' failed.\n", loc.file_name(), loc.line(), loc.function_name(), assertion);
     fwrite(str.data(), 1, str.size(), stderr);
diff --git a/src/util/check.h b/src/util/check.h
index 4a7a52b6a8..3ffe8af282 100644
--- a/src/util/check.h
+++ b/src/util/check.h
@@ -57,13 +57,12 @@ struct test_only_CheckFailuresAreExceptionsNotAborts {
 };
 
 std::string StrFormatInternalBug(std::string_view msg, const std::source_location& loc);
+std::string StrFormatFailedCheck(std::string_view assertion);
 
 class NonFatalCheckError : public std::runtime_error
 {
 public:
     NonFatalCheckError(std::string_view msg, const std::source_location& loc);
-    /// Create an instance in case of a failed assert condition, message is constructed accordingly
-    static NonFatalCheckError CreateFailedAssert(std::string_view assertion, const std::source_location& loc);
 };
 
 /** Internal helper */
@@ -77,7 +76,7 @@ T&& inline_check_non_fatal(LIFETIMEBOUND T&& val, const std::source_location& lo
         if constexpr (G_ABORT_ON_FAILED_ASSUME) {
             assertion_fail(loc, assertion);
         }
-        throw NonFatalCheckError::CreateFailedAssert(assertion, loc);
+        throw NonFatalCheckError{StrFormatFailedCheck(assertion), loc};
     }
     return std::forward<T>(val);
 }
Could we avoid adding a public `NonFatalCheckError` factory? The new formatting is only used by Assert, Assume, and CHECK_NONFATAL, so a helper for the formatted string keeps the generic exception constructor as the only public error API: ```patch diff --git a/src/test/util_check_tests.cpp b/src/test/util_check_tests.cpp index 3a47ecb3c2..49954dff9c 100644 --- a/src/test/util_check_tests.cpp +++ b/src/test/util_check_tests.cpp @@ -34,13 +34,4 @@ BOOST_AUTO_TEST_CASE(check_fail) BOOST_CHECK_EXCEPTION(Assert(this_should_be_nonnull != nullptr), NonFatalCheckError, HasReason{"Internal bug detected: Failed: 'this_should_be_nonnull != nullptr'"}); } -BOOST_AUTO_TEST_CASE(check_exception_constructor) -{ - const auto assert_text{"pointer != nullptr"}; - const auto location{std::source_location::current()}; - - BOOST_CHECK(std::string_view{NonFatalCheckError("some_message", location).what()}.find("Internal bug detected: some_message\n") != std::string_view::npos); - BOOST_CHECK(std::string_view{NonFatalCheckError::CreateFailedAssert(assert_text, location).what()}.find("Internal bug detected: Failed: 'pointer != nullptr'\n") != std::string_view::npos); -} - BOOST_AUTO_TEST_SUITE_END() diff --git a/src/util/check.cpp b/src/util/check.cpp index 36885d6db1..e26f3d7b41 100644 --- a/src/util/check.cpp +++ b/src/util/check.cpp @@ -15,6 +15,11 @@ #include <string> #include <string_view> +std::string StrFormatFailedCheck(std::string_view assertion) +{ + return strprintf("Failed: '%s'", assertion); +} + std::string StrFormatInternalBug(std::string_view msg, const std::source_location& loc) { return strprintf("Internal bug detected: %s\n%s:%d (%s)\n" @@ -29,17 +34,12 @@ NonFatalCheckError::NonFatalCheckError(std::string_view msg, const std::source_l { } -NonFatalCheckError NonFatalCheckError::CreateFailedAssert(std::string_view assertion, const std::source_location& loc) -{ - return NonFatalCheckError(strprintf("Failed: '%s'", assertion), loc); -} - bool g_detail_test_only_CheckFailuresAreExceptionsNotAborts{false}; void assertion_fail(const std::source_location& loc, std::string_view assertion) { if (g_detail_test_only_CheckFailuresAreExceptionsNotAborts) { - throw NonFatalCheckError::CreateFailedAssert(assertion, loc); + throw NonFatalCheckError{StrFormatFailedCheck(assertion), loc}; } auto str = strprintf("%s:%s %s: Assertion `%s' failed.\n", loc.file_name(), loc.line(), loc.function_name(), assertion); fwrite(str.data(), 1, str.size(), stderr); diff --git a/src/util/check.h b/src/util/check.h index 4a7a52b6a8..3ffe8af282 100644 --- a/src/util/check.h +++ b/src/util/check.h @@ -57,13 +57,12 @@ struct test_only_CheckFailuresAreExceptionsNotAborts { }; std::string StrFormatInternalBug(std::string_view msg, const std::source_location& loc); +std::string StrFormatFailedCheck(std::string_view assertion); class NonFatalCheckError : public std::runtime_error { public: NonFatalCheckError(std::string_view msg, const std::source_location& loc); - /// Create an instance in case of a failed assert condition, message is constructed accordingly - static NonFatalCheckError CreateFailedAssert(std::string_view assertion, const std::source_location& loc); }; /** Internal helper */ @@ -77,7 +76,7 @@ T&& inline_check_non_fatal(LIFETIMEBOUND T&& val, const std::source_location& lo if constexpr (G_ABORT_ON_FAILED_ASSUME) { assertion_fail(loc, assertion); } - throw NonFatalCheckError::CreateFailedAssert(assertion, loc); + throw NonFatalCheckError{StrFormatFailedCheck(assertion), loc}; } return std::forward<T>(val); } ```
l0rinc (Migrated from github.com) commented 2026-06-04 19:39:58 +00:00

Should the aborting assertion path use the same clarified wording? Without this, a normal fatal Assert still prints the old Assertion 'condition' failed shape while the exception path says the check failed.

diff --git a/src/util/check.cpp b/src/util/check.cpp
--- a/src/util/check.cpp	(revision e8c1542b132f855eecb2cf621eb280cd35d63cf0)
+++ b/src/util/check.cpp	(revision 4ffb91e3bbf0a06166b2a7bbd7f543d2f5c6b36d)
@@ -41,7 +41,7 @@
     if (g_detail_test_only_CheckFailuresAreExceptionsNotAborts) {
         throw NonFatalCheckError{StrFormatFailedCheck(assertion), loc};
     }
-    auto str = strprintf("%s:%s %s: Assertion `%s' failed.\n", loc.file_name(), loc.line(), loc.function_name(), assertion);
+    auto str = strprintf("%s:%s %s: Assertion failed: %s.\n", loc.file_name(), loc.line(), loc.function_name(), StrFormatFailedCheck(assertion));
     fwrite(str.data(), 1, str.size(), stderr);
     std::abort();
 }
Should the aborting assertion path use the same clarified wording? Without this, a normal fatal Assert still prints the old Assertion 'condition' failed shape while the exception path says the check failed. ```patch diff --git a/src/util/check.cpp b/src/util/check.cpp --- a/src/util/check.cpp (revision e8c1542b132f855eecb2cf621eb280cd35d63cf0) +++ b/src/util/check.cpp (revision 4ffb91e3bbf0a06166b2a7bbd7f543d2f5c6b36d) @@ -41,7 +41,7 @@ if (g_detail_test_only_CheckFailuresAreExceptionsNotAborts) { throw NonFatalCheckError{StrFormatFailedCheck(assertion), loc}; } - auto str = strprintf("%s:%s %s: Assertion `%s' failed.\n", loc.file_name(), loc.line(), loc.function_name(), assertion); + auto str = strprintf("%s:%s %s: Assertion failed: %s.\n", loc.file_name(), loc.line(), loc.function_name(), StrFormatFailedCheck(assertion)); fwrite(str.data(), 1, str.size(), stderr); std::abort(); } ```
optout21 (Migrated from github.com) reviewed 2026-06-17 12:46:41 +00:00
optout21 (Migrated from github.com) left a comment

Thanks for reviewing, @l0rinc; suggestions applied. Summary of changes:

  • Wording: Get rid of double prefix, "Internal bug detected: 'pindex != nullptr' check failed" instead of "Internal bug detected: Failed: 'pindex != nullptr'"
  • For formatting, use a standalone helper instead of a special factory for the exception
  • In assertion_fail, for the non-test-only abort path, also use the same wording.
  • Test: add test for NONFATAL_UNREACHABLE (no assert).

(git diff d46f1fae63ab8d81731ccf0a563119d9edeeeef8 00b0b0f68a825864481103a16caf2a64559f6951)

Thanks for reviewing, @l0rinc; suggestions applied. Summary of changes: - Wording: Get rid of double prefix, "Internal bug detected: 'pindex != nullptr' check failed" instead of "Internal bug detected: Failed: 'pindex != nullptr'" - For formatting, use a standalone helper instead of a special factory for the exception - In `assertion_fail`, for the non-test-only abort path, also use the same wording. - Test: add test for `NONFATAL_UNREACHABLE` (no assert). (`git diff d46f1fae63ab8d81731ccf0a563119d9edeeeef8 00b0b0f68a825864481103a16caf2a64559f6951`)
optout21 (Migrated from github.com) commented 2026-06-17 12:44:23 +00:00

Good point. Unfortunately, this is not possible/easy to test in a unit test (due to the abort).

Good point. Unfortunately, this is not possible/easy to test in a unit test (due to the abort).
l0rinc (Migrated from github.com) reviewed 2026-08-27 17:53:35 +00:00
l0rinc (Migrated from github.com) left a comment

Forgot about this one, please ping me next time if I don't reply in time (thanks @sedited)

Forgot about this one, please ping me next time if I don't reply in time (thanks @sedited)
l0rinc (Migrated from github.com) commented 2026-08-27 17:51:15 +00:00

Single quotes around a stringized check collide with character literals in real conditions such as Assert(cmd.at(0) != '-'), yielding ... 'cmd.at(0) != '-'' check failed.

Could we rather use backticks as the outer delimiters so the condition remains visually distinct?

Single quotes around a stringized check collide with character literals in real conditions such as `Assert(cmd.at(0) != '-')`, yielding `... 'cmd.at(0) != '-'' check failed`. Could we rather use backticks as the outer delimiters so the condition remains visually distinct?
l0rinc (Migrated from github.com) commented 2026-08-27 17:52:30 +00:00

The fatal path currently renders "Assertion failed: <condition> check failed", repeating the same outcome around the condition.

Could we render "Assertion <condition> check failed" once?

    auto str = strprintf("%s:%s %s: Assertion %s.\n", loc.file_name(), loc.line(), loc.function_name(), StrFormatFailedCheck(assertion));
The fatal path currently renders "Assertion failed: `<condition>` check failed", repeating the same outcome around the condition. Could we render "Assertion `<condition>` check failed" once? ```suggestion auto str = strprintf("%s:%s %s: Assertion %s.\n", loc.file_name(), loc.line(), loc.function_name(), StrFormatFailedCheck(assertion)); ```
optout21 (Migrated from github.com) reviewed 2026-09-01 15:37:33 +00:00
optout21 (Migrated from github.com) left a comment

Thanks for the review, @l0rinc; minor message string changes applied.

git diff 952c64ca50be450ed6288ca85a5a092a85001b42 4b63136bea857e8133c0ff3885fa9ec8d83c03ad

Thanks for the review, @l0rinc; minor message string changes applied. `git diff 952c64ca50be450ed6288ca85a5a092a85001b42 4b63136bea857e8133c0ff3885fa9ec8d83c03ad`
optout21 (Migrated from github.com) commented 2026-09-01 15:36:14 +00:00

Good point, applied.

Good point, applied.
optout21 (Migrated from github.com) commented 2026-09-01 15:36:26 +00:00

Applied.

Applied.
DrahtBot commented 2026-09-01 16:49:13 +00:00 (Migrated from github.com)

🚧 At least one of the CI tasks failed.
Task Alpine (musl): https://github.com/bitcoin/bitcoin/actions/runs/33526907826/job/99919961765
LLM reason (✨ experimental): CI failed due to a compiler crash (internal compiler error: Segmentation fault) while building the fuzz target (cmpctblock.cpp).

Hints

Try to run the tests locally, according to the documentation. However, a CI failure may still
happen due to a number of reasons, for example:

  • Possibly due to a silent merge conflict (the changes in this pull request being
    incompatible with the current code in the target branch). If so, make sure to rebase on the latest
    commit of the target branch.

  • A sanitizer issue, which can only be found by compiling with the sanitizer and running the
    affected test.

  • An intermittent issue.

Leave a comment here, if you need help tracking down a confusing failure.

<!--85328a0da195eb286784d51f73fa0af9--> 🚧 At least one of the CI tasks failed. <sub>Task `Alpine (musl)`: https://github.com/bitcoin/bitcoin/actions/runs/33526907826/job/99919961765</sub> <sub>LLM reason (✨ experimental): CI failed due to a compiler crash (internal compiler error: Segmentation fault) while building the fuzz target (`cmpctblock.cpp`).</sub> <details><summary>Hints</summary> Try to run the tests locally, according to the documentation. However, a CI failure may still happen due to a number of reasons, for example: * Possibly due to a silent merge conflict (the changes in this pull request being incompatible with the current code in the target branch). If so, make sure to rebase on the latest commit of the target branch. * A sanitizer issue, which can only be found by compiling with the sanitizer and running the affected test. * An intermittent issue. Leave a comment here, if you need help tracking down a confusing failure. </details>
l0rinc commented 2026-09-01 17:43:53 +00:00 (Migrated from github.com)

tested ACK 4b63136bea857e8133c0ff3885fa9ec8d83c03ad

The Alpine failure seems unrelated.
The PR description still shows the old Failed: '<condition>' wording and should be updated to match the current output.

tested ACK 4b63136bea857e8133c0ff3885fa9ec8d83c03ad The Alpine failure seems unrelated. The PR description still shows the old `Failed: '<condition>'` wording and should be updated to match the current output.
maflcko (Migrated from github.com) approved these changes 2026-09-01 18:49:31 +00:00
maflcko (Migrated from github.com) left a comment

lgtm, left two nits

lgtm, left two nits
maflcko (Migrated from github.com) commented 2026-09-01 18:47:18 +00:00

nit: Probably no need to repeat this. Could just adjust the above?

nit: Probably no need to repeat this. Could just adjust the above?
maflcko (Migrated from github.com) commented 2026-09-01 18:46:48 +00:00

Since you are adding this test, I wonder if the impl should be #define NONFATAL_UNREACHABLE inline_check_non_fatal(false, current(), "unreachable"), so that G_ABORT_ON_FAILED_ASSUME is recognized, just like for CHECK_NONFATAL.

Since you are adding this test, I wonder if the impl should be `#define NONFATAL_UNREACHABLE inline_check_non_fatal(false, current(), "unreachable")`, so that `G_ABORT_ON_FAILED_ASSUME` is recognized, just like for `CHECK_NONFATAL`.
optout21 commented 2026-09-02 08:45:40 +00:00 (Migrated from github.com)

The PR description still shows the old [...] wording ...

Done. (Thanks for the hint; commit message was updated but not the PR desc.)

> The PR description still shows the old [...] wording ... Done. (Thanks for the hint; commit message was updated but not the PR desc.)
optout21 (Migrated from github.com) reviewed 2026-09-02 10:12:58 +00:00
optout21 (Migrated from github.com) left a comment

Thanks for the comments, @maflcko . One nit applied, the other I've tried, but haven't included currently.

Thanks for the comments, @maflcko . One nit applied, the other I've tried, but haven't included currently.
optout21 (Migrated from github.com) commented 2026-09-02 09:55:38 +00:00

Applied.

Applied.
optout21 (Migrated from github.com) commented 2026-09-02 10:11:29 +00:00

This suggestion makes sense, however I'm a bit reluctant to include it in this PR, which is only about message rewording.
I did the suggestion here: github.com/optout21/bitcoin@5ac866eb21

Please advise (change it / make new PR with it / include in this PR).

This suggestion makes sense, however I'm a bit reluctant to include it in this PR, which is only about message rewording. I did the suggestion here: https://github.com/optout21/bitcoin/commit/5ac866eb21736deb7f6a616aebd066e6d8ec1a80 Please advise (change it / make new PR with it / include in this PR).
maflcko (Migrated from github.com) reviewed 2026-09-02 10:23:29 +00:00
maflcko (Migrated from github.com) commented 2026-09-02 10:23:29 +00:00

I really think this can just be a call to inline_check_non_fatal(false, current(), "unreachable") with the benefits:

  • If the G_ABORT_ON_FAILED_ASSUME logic ever changes again in the future, it is more likely to apply to all places. (Less code, logic and functions to keep track of)
  • It is less code and the compiler can inline and flatten the false literal by itself.
  • All of the macros here are for internal bugs, which should never happen, so the "unreachable" string is fine and sufficient for a dev-only debug-only string.

Personally I think it is fine to cycle this into this pull, but no strong opinion.

I really think this can just be a call to `inline_check_non_fatal(false, current(), "unreachable")` with the benefits: * If the `G_ABORT_ON_FAILED_ASSUME` logic ever changes again in the future, it is more likely to apply to all places. (Less code, logic and functions to keep track of) * It is less code and the compiler can inline and flatten the `false` literal by itself. * All of the macros here are for internal bugs, which should never happen, so the `"unreachable"` string is fine and sufficient for a dev-only debug-only string. Personally I think it is fine to cycle this into this pull, but no strong opinion.
optout21 (Migrated from github.com) reviewed 2026-09-02 11:34:38 +00:00
optout21 (Migrated from github.com) commented 2026-09-02 11:34:38 +00:00

A technicality: the inline_check_non_fatal was duplicated into a [[noreturn]] check_non_fatal_unreachable to avoid the compiler giving "control reaches end of non-void function [-Wreturn-type]" warnings in a few places where NONFATAL_UNREACHABLE is used. While the macro was a simple throw, it could be used as a last statement in a non-void method; with the new if this is not clear for the compiler, hence the need for a [[noreturn]] marker (which cannot be applied on inline_check_non_fatal).

A technicality: the `inline_check_non_fatal` was duplicated into a `[[noreturn]]` `check_non_fatal_unreachable` to avoid the compiler giving "control reaches end of non-void function [-Wreturn-type]" warnings in a few places where `NONFATAL_UNREACHABLE` is used. While the macro was a simple `throw`, it could be used as a last statement in a non-void method; with the new `if` this is not clear for the compiler, hence the need for a `[[noreturn]]` marker (which cannot be applied on `inline_check_non_fatal`).
maflcko (Migrated from github.com) reviewed 2026-09-02 11:53:34 +00:00
maflcko (Migrated from github.com) commented 2026-09-02 11:53:34 +00:00

Right. In that case, it could make sense to have a single [[noreturn]] void check_non_fatal_fai(const std::source_location& loc, std::string_view err_msg); helper. And then call it in both places:

  • In inline_check_non_fatal: check_non_fatal_fai(loc, assertion);
  • And in: #define NONFATAL_UNREACHABLE check_non_fatal_fai( current(), "unreachable")
Right. In that case, it could make sense to have a single `[[noreturn]] void check_non_fatal_fai(const std::source_location& loc, std::string_view err_msg);` helper. And then call it in both places: * In inline_check_non_fatal: `check_non_fatal_fai(loc, assertion);` * And in: `#define NONFATAL_UNREACHABLE check_non_fatal_fai( current(), "unreachable")`
optout21 commented 2026-09-03 11:57:27 +00:00 (Migrated from github.com)

Reworked based on @maflcko's suggestion, and unified the non-fatal unreachable and check behavior, so that unreachable also honors special abort setting (G_ABORT_ON_FAILED_ASSUME).
Added two more commits (but also adjusted the first to be in sync with the latter ones).

  • Error cases with an assert include the "'condition' check failed" message (but not the unreachable error).
  • Non-fatal unreachable and check cases throw, but abort if G_ABORT_ON_FAILED_ASSUME is set (and thwow if test g_detail_test_only_CheckFailuresAreExceptionsNotAborts is set).
  • The condition checking G_ABORT_ON_FAILED_ASSUME has been extracted into a new helper check_non_fatal_fail(), so it can be reused.
  • The internal helper assertion_fail() has been renamed to the more descriptive internal_abort_helper().
Reworked based on @maflcko's suggestion, and unified the non-fatal unreachable and check behavior, so that unreachable also honors special abort setting (G_ABORT_ON_FAILED_ASSUME). Added two more commits (but also adjusted the first to be in sync with the latter ones). - Error cases with an assert include the "'condition' check failed" message (but not the unreachable error). - Non-fatal unreachable and check cases throw, but abort if G_ABORT_ON_FAILED_ASSUME is set (and thwow if test `g_detail_test_only_CheckFailuresAreExceptionsNotAborts` is set). - The condition checking `G_ABORT_ON_FAILED_ASSUME` has been extracted into a new helper `check_non_fatal_fail()`, so it can be reused. - The internal helper `assertion_fail()` has been renamed to the more descriptive `internal_abort_helper()`.
optout21 commented 2026-09-03 17:08:20 +00:00 (Migrated from github.com)

Ready for review. CI failure seems unrelated (MacOS, device out of space).

Ready for review. CI failure seems unrelated (MacOS, device out of space).
optout21 (Migrated from github.com) reviewed 2026-09-03 17:08:47 +00:00
optout21 (Migrated from github.com) commented 2026-09-03 17:08:47 +00:00

Suggestion applied.

Suggestion applied.
l0rinc (Migrated from github.com) reviewed 2026-09-07 16:57:24 +00:00
l0rinc (Migrated from github.com) commented 2026-09-07 16:57:24 +00:00

Please update the PR description after this change

Please update the PR description after this change
l0rinc commented 2026-09-07 16:58:46 +00:00 (Migrated from github.com)

code review reACK 6494a7e1c8

The PR description still needs update to use backticks and about NONFATAL_UNREACHABLE not being affected

code review reACK 6494a7e1c863321a5d787ff0e50a51a2e34e9970 The PR description still needs update to use backticks and about `NONFATAL_UNREACHABLE` not being affected
optout21 commented 2026-09-07 17:36:09 +00:00 (Migrated from github.com)

The PR description still needs update

Description updated, remaining inconsistencies fixed, thanks.

> The PR description still needs update Description updated, remaining inconsistencies fixed, thanks.
optout21 commented 2026-10-19 11:31:25 +00:00 (Migrated from github.com)

Still awaiting review.

Still awaiting review.
First-time contributor

Base: 4303f3b7f0775cdbbe77faddf142e1e44fa590ca
Head: 81fde00cc7ff2c5ae85e8601d8b780ddde7dc2a6

Findings

Suggestions

Test the abort path without the exception override (src/test/util_check_tests.cpp:44, head)

This test enables the exception override before calling NONFATAL_UNREACHABLE(). With aborts enabled, internal_abort_helper() then throws instead of aborting, so the test checks the diagnostic but not the abort behavior. It would still pass if the abort-policy branch were removed. Keep this diagnostic test and add a subprocess or death test with the override disabled in an abort-enabled configuration.

Review coverage was incomplete.

Review limitations
  • design: The RPC reachability of the NONFATAL_UNREACHABLE call sites in src/wallet/rpc/backup.cpp:293 and src/wallet/rpc/spend.cpp:1133 was not checked. Whether RPC-controlled state can reach them could change the assessment of the new debug-mode abort behavior.

Full review report

<!-- forgejo-review-bot:bitcoin/bitcoin --> Base: `4303f3b7f0775cdbbe77faddf142e1e44fa590ca` Head: `81fde00cc7ff2c5ae85e8601d8b780ddde7dc2a6` ##### Findings **Suggestions** **Test the abort path without the exception override** (src/test/util_check_tests.cpp:44, head) This test enables the exception override before calling `NONFATAL_UNREACHABLE()`. With aborts enabled, `internal_abort_helper()` then throws instead of aborting, so the test checks the diagnostic but not the abort behavior. It would still pass if the abort-policy branch were removed. Keep this diagnostic test and add a subprocess or death test with the override disabled in an abort-enabled configuration. Review coverage was incomplete. <details> <summary>Review limitations</summary> - design: The RPC reachability of the NONFATAL_UNREACHABLE call sites in src/wallet/rpc/backup.cpp:293 and src/wallet/rpc/spend.cpp:1133 was not checked. Whether RPC-controlled state can reach them could change the assessment of the new debug-mode abort behavior. </details> [Full review report](<https://review.fish.foo/traces/35461-81fde00cc7ff2c5ae85e8601d8b780ddde7dc2a6-946-3.html>)
maflcko (Migrated from github.com) approved these changes 2026-10-19 13:33:32 +00:00
maflcko (Migrated from github.com) left a comment

review ACK 6494a7e1c8 🍅

Show signature

Signature:

untrusted comment: signature from minisign secret key on empty file; verify via: minisign -Vm "${path_to_any_empty_file}" -P RWTRmVTMeKV5noAMqVlsMugDDCyyTSbA3Re5AkUrhvLVln0tSaFWglOw -x "${path_to_this_whole_four_line_signature_blob}"
RUTRmVTMeKV5npGrKx1nqXCw5zeVHdtdYURB/KlyA/LMFgpNCs+SkW9a8N95d+U4AP1RJMi+krxU1A3Yux4bpwZNLvVBKy0wLgM=
trusted comment: review ACK 6494a7e1c863321a5d787ff0e50a51a2e34e9970 🍅
l6phmxcotFO8icaJX33SEkPwdw6zXxX8HiXEDwNgGOJhlTrcCiamd4Rl8qiEe5pheXzoWxWaxIC3j2SZ/LZkDg==
review ACK 6494a7e1c863321a5d787ff0e50a51a2e34e9970 🍅 <details><summary>Show signature</summary> Signature: ``` untrusted comment: signature from minisign secret key on empty file; verify via: minisign -Vm "${path_to_any_empty_file}" -P RWTRmVTMeKV5noAMqVlsMugDDCyyTSbA3Re5AkUrhvLVln0tSaFWglOw -x "${path_to_this_whole_four_line_signature_blob}" RUTRmVTMeKV5npGrKx1nqXCw5zeVHdtdYURB/KlyA/LMFgpNCs+SkW9a8N95d+U4AP1RJMi+krxU1A3Yux4bpwZNLvVBKy0wLgM= trusted comment: review ACK 6494a7e1c863321a5d787ff0e50a51a2e34e9970 🍅 l6phmxcotFO8icaJX33SEkPwdw6zXxX8HiXEDwNgGOJhlTrcCiamd4Rl8qiEe5pheXzoWxWaxIC3j2SZ/LZkDg== ``` </details>
maflcko (Migrated from github.com) commented 2026-10-19 13:23:19 +00:00

nit in 0d67983acd: I think users will see that the program aborted after writing this, so the Assertion: could also be removed. This means the msgs will be:

  • [location] bla check failed.
  • and [location] unreachable.

which seems fine.

nit in 0d67983acdfe3ccdb9147994ca05383f5c0d1d41: I think users will see that the program aborted after writing this, so the `Assertion: ` could also be removed. This means the msgs will be: * [location] `bla` check failed. * and [location] unreachable. which seems fine.
Lines 72-73
maflcko (Migrated from github.com) commented 2026-10-19 13:26:24 +00:00

nit in 6494a7e1c8: This is my fault for suggesting the name, but I think a better name would be internal_non_fatal_helper. with the docstring:

/// Internal helper. The noreturn enables optimizers to discard invalid paths.

(like above)

There is no need to list all call sites. If someone wanted to know them, they could grep.

nit in 6494a7e1c863321a5d787ff0e50a51a2e34e9970: This is my fault for suggesting the name, but I think a better name would be `internal_non_fatal_helper`. with the docstring: ``` /// Internal helper. The noreturn enables optimizers to discard invalid paths. ``` (like above) There is no need to list all call sites. If someone wanted to know them, they could grep.
Lines 114-115
maflcko (Migrated from github.com) commented 2026-10-19 13:32:45 +00:00

6494a7e1c8: Same

6494a7e1c863321a5d787ff0e50a51a2e34e9970: Same
Lines 139-140
maflcko (Migrated from github.com) commented 2026-10-19 13:32:34 +00:00

I think it would be good to keep the docs symmetric with Assume which uses the wording "In debug builds it behaves ...`. So the wording here could simply be: "In debug builds an abort is triggered"

I think it would be good to keep the docs symmetric with `Assume` which uses the wording "In debug builds it behaves ...`. So the wording here could simply be: "In debug builds an abort is triggered"
l0rinc commented 2026-10-19 16:09:01 +00:00 (Migrated from github.com)

Happy to re-review if you take the suggestions

Happy to re-review if you take the suggestions
optout21 (Migrated from github.com) reviewed 2026-10-20 18:45:00 +00:00
optout21 (Migrated from github.com) left a comment

@maflcko, thanks for review suggestions, reviewed and applied all.

git range-diff 6494a7e1c863321a5d787ff0e50a51a2e34e9970...704ce3a7adb7d2b67082c5fe8fc328a2d2d43891

@maflcko, thanks for review suggestions, reviewed and applied all. `git range-diff 6494a7e1c863321a5d787ff0e50a51a2e34e9970...704ce3a7adb7d2b67082c5fe8fc328a2d2d43891`
optout21 (Migrated from github.com) commented 2026-10-20 18:36:48 +00:00

Good point, taken

Good point, taken
Lines 72-73
optout21 (Migrated from github.com) commented 2026-10-20 18:37:07 +00:00

Done

Done
Lines 114-115
optout21 (Migrated from github.com) commented 2026-10-20 18:37:23 +00:00

Done

Done
Lines 139-140
optout21 (Migrated from github.com) commented 2026-10-20 18:37:15 +00:00

Done

Done
l0rinc commented 2026-10-20 19:05:11 +00:00 (Migrated from github.com)

Could you rebase and update util_not_null_tests to expect Internal bug detected: `m_ptr != nullptr` check failed? The stale expectation is causing the CI failures.

Please also drop the description's Note: … are not affected sentence, since the NONFATAL_UNREACHABLE message changes to unreachable.

Could you rebase and update `util_not_null_tests` to expect ```Internal bug detected: `m_ptr != nullptr` check failed```? The stale expectation is causing the CI failures. Please also drop the description's `Note: … are not affected` sentence, since the `NONFATAL_UNREACHABLE` message changes to `unreachable`.
optout21 commented 2026-10-20 19:21:12 +00:00 (Migrated from github.com)

A recently introduced unit test failed in CI (util_not_null_tests). Rebased and adjusted the test.
Also adjusted the description, according to @l0rinc's finding.

A recently introduced unit test failed in CI (`util_not_null_tests`). Rebased and adjusted the test. Also adjusted the description, according to @l0rinc's finding.
DrahtBot commented 2026-10-20 19:21:48 +00:00 (Migrated from github.com)

🚧 At least one of the CI tasks failed.
Task macOS native: https://github.com/bitcoin/bitcoin/actions/runs/39477470807/job/118817050199
LLM reason (✨ experimental): CI failed because util_not_null_tests had a failing exception check in check_null_smart_pointer (the HasReason validation raised NonFatalCheckError).

Hints

Try to run the tests locally, according to the documentation. However, a CI failure may still
happen due to a number of reasons, for example:

  • Possibly due to a silent merge conflict (the changes in this pull request being
    incompatible with the current code in the target branch). If so, make sure to rebase on the latest
    commit of the target branch.

  • A sanitizer issue, which can only be found by compiling with the sanitizer and running the
    affected test.

  • An intermittent issue.

Leave a comment here, if you need help tracking down a confusing failure.

<!--85328a0da195eb286784d51f73fa0af9--> 🚧 At least one of the CI tasks failed. <sub>Task `macOS native`: https://github.com/bitcoin/bitcoin/actions/runs/39477470807/job/118817050199</sub> <sub>LLM reason (✨ experimental): CI failed because `util_not_null_tests` had a failing exception check in `check_null_smart_pointer` (the `HasReason` validation raised `NonFatalCheckError`).</sub> <details><summary>Hints</summary> Try to run the tests locally, according to the documentation. However, a CI failure may still happen due to a number of reasons, for example: * Possibly due to a silent merge conflict (the changes in this pull request being incompatible with the current code in the target branch). If so, make sure to rebase on the latest commit of the target branch. * A sanitizer issue, which can only be found by compiling with the sanitizer and running the affected test. * An intermittent issue. Leave a comment here, if you need help tracking down a confusing failure. </details>
l0rinc commented 2026-10-20 20:22:30 +00:00 (Migrated from github.com)

ACK 81fde00cc7

Thanks for the quick updates!

ACK 81fde00cc7ff2c5ae85e8601d8b780ddde7dc2a6 Thanks for the quick updates!
maflcko commented 2026-10-21 07:50:15 +00:00 (Migrated from github.com)

review ACK 81fde00cc7 🎷

Show signature

Signature:

untrusted comment: signature from minisign secret key on empty file; verify via: minisign -Vm "${path_to_any_empty_file}" -P RWTRmVTMeKV5noAMqVlsMugDDCyyTSbA3Re5AkUrhvLVln0tSaFWglOw -x "${path_to_this_whole_four_line_signature_blob}"
RUTRmVTMeKV5npGrKx1nqXCw5zeVHdtdYURB/KlyA/LMFgpNCs+SkW9a8N95d+U4AP1RJMi+krxU1A3Yux4bpwZNLvVBKy0wLgM=
trusted comment: review ACK 81fde00cc7ff2c5ae85e8601d8b780ddde7dc2a6 🎷
0TxKh5I3IF3ukLe+anKfRPCtJElRV6DwBpCPbZXz5fiU6BBn0vnJjPF8xW7/xt4Sc0ox0p0EVEs+9iHmvPIQAw==
review ACK 81fde00cc7ff2c5ae85e8601d8b780ddde7dc2a6 🎷 <details><summary>Show signature</summary> Signature: ``` untrusted comment: signature from minisign secret key on empty file; verify via: minisign -Vm "${path_to_any_empty_file}" -P RWTRmVTMeKV5noAMqVlsMugDDCyyTSbA3Re5AkUrhvLVln0tSaFWglOw -x "${path_to_this_whole_four_line_signature_blob}" RUTRmVTMeKV5npGrKx1nqXCw5zeVHdtdYURB/KlyA/LMFgpNCs+SkW9a8N95d+U4AP1RJMi+krxU1A3Yux4bpwZNLvVBKy0wLgM= trusted comment: review ACK 81fde00cc7ff2c5ae85e8601d8b780ddde7dc2a6 🎷 0TxKh5I3IF3ukLe+anKfRPCtJElRV6DwBpCPbZXz5fiU6BBn0vnJjPF8xW7/xt4Sc0ox0p0EVEs+9iHmvPIQAw== ``` </details>
sedited (Migrated from github.com) approved these changes 2026-10-21 08:35:02 +00:00
sedited (Migrated from github.com) left a comment
ACK 81fde00cc7ff2c5ae85e8601d8b780ddde7dc2a6
Sign in to join this conversation.
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
bitcoin/bitcoin!35461
No description provided.