Skip to content

util/check: Add CHECK_NONFATAL identity function and NONFATAL_UNREACHABLE macro #24812

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 24, 2022

Conversation

aureleoules
Copy link
Contributor

@aureleoules aureleoules commented Apr 8, 2022

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: #24804 (comment).

Also adds UNREACHABLE_NONFATAL macro.

@aureleoules aureleoules force-pushed the check_non_fatal_identity branch 2 times, most recently from 70bff71 to 5eeb1e5 Compare April 9, 2022 00:14
@aureleoules aureleoules force-pushed the check_non_fatal_identity branch 3 times, most recently from ebdd846 to 0661baf Compare April 10, 2022 00:24
@aureleoules aureleoules changed the title util/check: Add CheckNonFatal identity function and use it in src/rpc util/check: Replace CHECK_NONFATAL macro with identity function and use it in src/rpc Apr 10, 2022
@bitcoin bitcoin deleted a comment from joshuadbryant1985 Apr 10, 2022
@DrahtBot
Copy link
Contributor

DrahtBot commented Apr 11, 2022

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

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #24455 (refactor: Split ArgsManager out of util/system by Empact)
  • #22341 (rpc: add getxpub by Sjors)
  • #19792 (rpc: Add dumpcoinstats by fjahr)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

Copy link

@vincenzopalazzo vincenzopalazzo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, but I'm not a big fan of while(0) in C++, maybe C in yes

Copy link
Member

@maflcko maflcko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@aureleoules aureleoules force-pushed the check_non_fatal_identity branch from 3665422 to 1e4d382 Compare April 12, 2022 11:46
@aureleoules
Copy link
Contributor Author

I've addressed the nits, thanks for your reviews.

@aureleoules aureleoules changed the title util/check: Replace CHECK_NONFATAL macro with identity function and use it in src/rpc util/check: Add CHECK_NONFATAL identity function, NONFATAL_UNREACHABLE AND UNREACHABLE macros Apr 12, 2022
Copy link

@vincenzopalazzo vincenzopalazzo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 1e4d382

Good job!

Copy link
Member

@jonatack jonatack left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 1e4d382

Tested NONFATAL_UNREACHABLE

error code: -1
error message:
Internal bug detected: 'Unreachable code reached'
wallet/rpc/addresses.cpp:38 (operator())
Please report this issue here: https://github.com/bitcoin/bitcoin/issues

Tested UNREACHABLE

Internal bug detected: 'Unreachable code reached'
wallet/rpc/addresses.cpp:38 (operator())
Please report this issue here: https://github.com/bitcoin/bitcoin/issues

Aborted

If you retouch, it looks like #include <util/check.h> ought to be added to /rpc/blockchain.cpp and /rpc/util.cpp as well here.

@aureleoules aureleoules force-pushed the check_non_fatal_identity branch 2 times, most recently from f9cffe2 to 63b7534 Compare April 13, 2022 14:13
@jonatack
Copy link
Member

re-ACK 63b7534

@maflcko
Copy link
Member

maflcko commented Apr 15, 2022

Might be good to adjust the rpc linter as well for the new macros: #24856

Can be done in a follow-up to avoid a conflict for now.

@aureleoules aureleoules force-pushed the check_non_fatal_identity branch from 63b7534 to ee02c8b Compare April 16, 2022 13:08
@aureleoules aureleoules changed the title util/check: Add CHECK_NONFATAL identity function, NONFATAL_UNREACHABLE AND UNREACHABLE macros util/check: Add CHECK_NONFATAL identity function and NONFATAL_UNREACHABLE macro Apr 16, 2022
@jonatack
Copy link
Member

ACK ee02c8b

Copy link
Member

@maflcko maflcko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this also fixes a warning with gcc-12.

ACK ee02c8b 🍨

Show signature

Signature:

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512

ACK ee02c8bd9aedad8cfd3c2618035fe275da025fb9 🍨
-----BEGIN PGP SIGNATURE-----

iQGzBAEBCgAdFiEE+rVPoUahrI9sLGYTzit1aX5ppUgFAlwqrYAACgkQzit1aX5p
pUjsdwwArddL1iP4wywDs5YPWiBmm1hOzJSazUrpOb4gx4plkEEHXOXvGqIIwoRQ
IPnUtiwBnIfZ7UDTHRUCgY8jeLNmlfZAtKRqd8n9V7fat6Uarmubynkz66O4C/pH
lPV6WRkkcby+KDy01EJW10vGa0NqLg9VoxWiTIfb5LwoiyqcxnLYvJ6/Ir30GYsn
cgG9pOIRhyYPYJQp79Q2R1fZmNe1Vi53RfxCBBkNXJI+tlMeCT7N8A6IHh239Dak
KPQP43FDbgkhTuwDmf+RNipSqyvH0sWRTqNCei9Q6WWqwGiiRMH883GWtWO392bR
gvkf+TmQeVoWqELVi+fFk7d+14zLJnnIvouMESjacwCEbkL5j5OKSw8WW6g0Uffb
y5VyRSEFV02OrxMHmKFKn3UEAQm551gnILgu0ERvkKopedGXxttNdvsnj9nuYiMj
IyPjD7MVuu+Ri6IaFtJ7jxCUGm+cPgei79bOuumTrRt/6g/+QZZnniZyhNx5oVHz
CKvY4MQC
=l4Sj
-----END PGP SIGNATURE-----

@@ -793,7 +794,7 @@ void RPCResult::ToSections(Sections& sections, const OuterType outer_type, const
return;
}
case Type::ANY: {
CHECK_NONFATAL(false); // Only for testing
NONFATAL_UNREACHABLE(); // Only for testing
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will also fix a warning on current master with gcc-12:

In file included from ./rpc/util.h:19,
                 from rpc/util.cpp:8:
rpc/util.cpp: In member function 'void RPCResult::ToSections(Sections&, OuterType, int) const':
./util/check.h:34:9: warning: this statement may fall through [-Wimplicit-fallthrough=]
   34 |         if (!(condition)) {                                       \
      |         ^~
rpc/util.cpp:796:9: note: in expansion of macro 'CHECK_NONFATAL'
  796 |         CHECK_NONFATAL(false); // Only for testing
      |         ^~~~~~~~~~~~~~
rpc/util.cpp:798:5: note: here
  798 |     case Type::NONE: {
      |     ^~~~

template <typename T>
T&& inline_check_non_fatal(T&& val, const char* file, int line, const char* func, const char* assertion)
{
if (!(val)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (!(val)) {
if (!val) {

nit: No need for ().

throw NonFatalCheckError(
format_internal_error(assertion, file, line, func, PACKAGE_BUGREPORT));
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: no need for newline

@maflcko maflcko merged commit b1c5991 into bitcoin:master Apr 24, 2022
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Apr 26, 2022
@aureleoules aureleoules deleted the check_non_fatal_identity branch May 20, 2022 22:14
Fabcien pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Feb 3, 2023
…CHABLE macro

Summary: This is a backport of [[bitcoin/bitcoin#24812 | core#24812]]

Test Plan: `ninja all check-all`

Reviewers: #bitcoin_abc, sdulfari

Reviewed By: #bitcoin_abc, sdulfari

Subscribers: sdulfari

Differential Revision: https://reviews.bitcoinabc.org/D13096
@bitcoin bitcoin locked and limited conversation to collaborators May 20, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants