-
Notifications
You must be signed in to change notification settings - Fork 37.7k
util/check: Don't use a lambda for Assert/Assume #24714
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, some nits.
// Check that Assert doesn't require copy/move | ||
NoCopyOrMove x{9}; | ||
Assert(x).i += 3; | ||
Assert(x).test(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe check the return value, if that was the point?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Point was mostly to call test()
This puts it in a function body, so that __func__ is available for reporting any assertion failure.
Nits picked |
Tested with diff: diff --git a/src/test/util_tests.cpp b/src/test/util_tests.cpp
index 1881573e7a..d7328ff450 100644
--- a/src/test/util_tests.cpp
+++ b/src/test/util_tests.cpp
@@ -84,7 +84,7 @@ BOOST_AUTO_TEST_CASE(util_check)
const std::unique_ptr<int> p_two = Assert(std::make_unique<int>(2));
// Check that Assert works on lvalues and rvalues
const int two = *Assert(p_two);
- Assert(two == 2);
+ Assert(two != 2);
Assert(true);
// Check that Assume can be used as unary expression
const bool result{Assume(two == 2)}; Before:
With this pull:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Left some more nits (can be ignored).
ACK 3bcffa1 🐤
Show signature
Signature:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512
ACK 3bcffa1146beb40e28bd1bcfaccf72e0d138c9c0 🐤
-----BEGIN PGP SIGNATURE-----
iQGzBAEBCgAdFiEE+rVPoUahrI9sLGYTzit1aX5ppUgFAlwqrYAACgkQzit1aX5p
pUgTQgv9HP5fwUc67QUWBjyLMXITzFCx7fUOKh97aePdHaegYMjauudEKiY5TOTi
Ks9HFnnirQmNq23B21wTGlkJBRkGIG16v3S5dS7iDGETGepXDBc1myBJv5u2AElG
l4IKxqWPVs9EUAzpDaNo9nxXrz0O3xPlUVDPA46zVl+IUDM8gZ3t8uqEoJAly7Jj
9FMreyD2riiKrZ9+dEIhAsJ08/Ski9lwxforRvVvk5a8dIztKgScjHWP+TcL5bTA
ICA6d5TF6Zbl1SpTgNFNM+Yw9rCiND+4Z/k8zDUYnrt0dF13I4uDHG1FX8xgTvL/
yxj/0/PC8gZfphOPWnL++06MMtKuplTY5TJD3pcC2MJZORU/fJp+w1RKvhaaf0/W
ZZzK2rj9kGeKOofSRKvqWq7zgd+8L8GK1xtV6AsCzuMGoEruXZlF0BI/CiLF1b0z
hzunflN07ltCAl0R2LpAJ1YDazE/vY+A25K6iktkAhXpB0+6WGNGFjk1BKeXU3lX
xDrgwPRG
=7L0O
-----END PGP SIGNATURE-----
Concept ACK, testing |
Looks like this also intentionally or accidentally fixes #21596, as the value is no longer evaluated inside the function, but before passing it to the function as parameter? Also, it fixes the nesting issue that is fixed only in C++20 (#23363)? Quite amazing that this relatively simple patch fixes three bugs. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK 3bcffa1
If #24672 is reverted with the following diff
--- a/src/init.cpp
+++ b/src/init.cpp
@@ -1541,7 +1541,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
// ********************************************************* Step 8: start indexers
if (args.GetBoolArg("-txindex", DEFAULT_TXINDEX)) {
- if (const auto error{WITH_LOCK(cs_main, return CheckLegacyTxindex(*Assert(chainman.m_blockman.m_block_tree_db)))}) {
+ if (const auto error{CheckLegacyTxindex(*Assert(chainman.m_blockman.m_block_tree_db))}) {
return InitError(*error);
}
then with this pull Clang thread safety analysis is now able to detect the missing lock that on current master was previously undetected
CXX node/libbitcoin_node_a-blockstorage.o
init.cpp:1544:77: error: passing variable 'm_block_tree_db' by reference requires holding mutex 'cs_main' [-Werror,-Wthread-safety-reference]
if (const auto error{CheckLegacyTxindex(*Assert(chainman.m_blockman.m_block_tree_db))}) {
^
1 error generated.
Nits picked again |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Couldn't find more nits.
ACK 2ef47ba 🚢
Show signature
Signature:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512
ACK 2ef47ba6c57a12840499a13908ab61aefca6cb55 🚢
-----BEGIN PGP SIGNATURE-----
iQGzBAEBCgAdFiEE+rVPoUahrI9sLGYTzit1aX5ppUgFAlwqrYAACgkQzit1aX5p
pUjUxwv7BclwGFAkVZZoIl0/IF1gA7gkGSNIRdHWw0oS+fOvd7SLO369yYsTParO
DdQ8FLTNoptoKsSJ2C6CyNl1zZ0WV7Wvndj66uFjsHntn5N4OWwBm+731zyBMRJo
xbOloJXrcLj5Aw1KXBCuKN4mylLN/PFythzdq2+VnAtjOTjw59JYKpqktfSQAXS3
+51l9BCZJf/F2G4pa1y8yKqyeuAr+IGsC/4SvFybXCaFLiRAR8A+4/TFsAmTJnYn
T3csvAXoLJDfdn0/mGV2d5pSlPwxqvIMJdhW01cm4Oj2BDgPdsSUizLiTATtTvIG
69a2nOO9mDBNbtfj7Wy/BATVRMQDVBxZeafdbCYtUO7/BjyAzAE7hSOODAMtv98p
XQFfDvhBZSsuYEaADQELDx1FqNn7fqz7I/NjiWZbz9+onINH8t+IDlO+qUcChQ3w
aIX/q1FjmZVxWtqf/kiKyann0Gx3plqjnBFSv/54P0K3D5s4yS1BWNSZOGyciGb+
Cm6dc3hb
=xsn6
-----END PGP SIGNATURE-----
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tested re-ACK 2ef47ba
Nice updates in the last push.
couple of nits
not worth changing unless you retouch for something else
diff --git a/src/test/util_tests.cpp b/src/test/util_tests.cpp
index b5d8411e1d..95d84593aa 100644
--- a/src/test/util_tests.cpp
+++ b/src/test/util_tests.cpp
@@ -79,9 +79,8 @@ BOOST_AUTO_TEST_CASE(util_datadir)
-class NoCopyOrMove
+struct NoCopyOrMove
{
-public:
int i;
explicit NoCopyOrMove(int i) : i{i} { }
diff --git a/src/util/check.cpp b/src/util/check.cpp
index 2a9f885560..56d66f78c6 100644
--- a/src/util/check.cpp
+++ b/src/util/check.cpp
@@ -8,7 +8,7 @@
void assertion_fail(const char* file, int line, const char* func, const char* assertion)
{
- auto str = strprintf("%s:%s %s: Assertion `%s' failed.\n", file, line, func, assertion);
+ const auto str = strprintf("%s:%s %s: Assertion `%s' failed.\n", file, line, func, assertion);
fwrite(str.data(), 1, str.size(), stderr);
std::abort();
}
This appears to have introduced build warnings for me on master, when building with gcc 9:
configure options:
Updating to gcc-10 removes the warnings. |
Other workarounds for gcc-9:
diff --git a/src/util/check.h b/src/util/check.h
index 80e973e7e..dea498293 100644
--- a/src/util/check.h
+++ b/src/util/check.h
@@ -53,6 +53,7 @@ void assertion_fail(const char* file, int line, const char* func, const char* as
template <bool IS_ASSERT, typename T>
T&& inline_assertion_check(T&& val, const char* file, int line, const char* func, const char* assertion)
{
+ (void)file,(void)line,(void)func,(void)assertion;
if constexpr (IS_ASSERT
#ifdef ABORT_ON_FAILED_ASSUME
|| true |
I'd vote for the third (mark them used), with a comment that this is to workaround behaviour in gcc-9 and can be removed when the minimum gcc version is 10 or higher. |
Looks like this is https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81676 which wasn't fixed until gcc 10 |
Looks like another workaround might be to say |
2ef47ba util/check: stop using lambda for Assert/Assume (Anthony Towns) 7c9fe25 wallet: move Assert() check into constructor (Anthony Towns) Pull request description: Using a lambda creates a couple of odd namespacing issues, in particular making clang's thread safety analysis less helpful, and confusing gcc when calling member functions. Fix this by not using a lambda. Fixes bitcoin#21596 Fixes bitcoin#24654 ACKs for top commit: MarcoFalke: ACK 2ef47ba 🚢 jonatack: Tested re-ACK 2ef47ba Tree-SHA512: 4bdbf3215f3d14472df0552362c5eebe8b7eea2d0928a8a41109edd4e0c5f95de6f8220eb2fee8506874e352c003907faf5ef344174795939306a618157b1bae
Using a lambda creates a couple of odd namespacing issues, in particular making clang's thread safety analysis less helpful, and confusing gcc when calling member functions. Fix this by not using a lambda.
Fixes #21596
Fixes #24654