Skip to content

Commit 7fd4905

Browse files
committed
Merge bitcoin#30235: build: warn on self-assignment
15796d4 build: warn on self-assignment (Cory Fields) 53372f2 refactor: disable self-assign warning for tests (Cory Fields) Pull request description: Belt-and suspenders after bitcoin#30234. Self-assignment should be safe _and_ discouraged. We used to opt out of this warning because something deep in our serialization/byteswapping code could self-assign, but that doesn't appear to be the case anymore. ACKs for top commit: maflcko: ACK 15796d4 fanquake: ACK 15796d4 - not a huge fan of inline pragma usage, but this seems fine, given it's to work around an already-fixed compiler bug, and we'll only be carrying it for a shortish time in any case. Tree-SHA512: 1f95f7c730b974ad1da55ebd381040bac312f2f380fff9d569ebab91d7c1963592a84d1613d81d96238c6f5a66aa40deebba68a76f6b24b02150d0a77c769654
2 parents ea88a75 + 15796d4 commit 7fd4905

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

configure.ac

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,12 +411,12 @@ AX_CHECK_COMPILE_FLAG([-Wsuggest-override], [WARN_CXXFLAGS="$WARN_CXXFLAGS -Wsug
411411
AX_CHECK_COMPILE_FLAG([-Wimplicit-fallthrough], [WARN_CXXFLAGS="$WARN_CXXFLAGS -Wimplicit-fallthrough"], [], [$CXXFLAG_WERROR])
412412
AX_CHECK_COMPILE_FLAG([-Wunreachable-code], [WARN_CXXFLAGS="$WARN_CXXFLAGS -Wunreachable-code"], [], [$CXXFLAG_WERROR])
413413
AX_CHECK_COMPILE_FLAG([-Wdocumentation], [WARN_CXXFLAGS="$WARN_CXXFLAGS -Wdocumentation"], [], [$CXXFLAG_WERROR])
414+
AX_CHECK_COMPILE_FLAG([-Wself-assign], [WARN_CXXFLAGS="$WARN_CXXFLAGS -Wself-assign"], [], [$CXXFLAG_WERROR])
414415

415416
dnl Some compilers (gcc) ignore unknown -Wno-* options, but warn about all
416417
dnl unknown options if any other warning is produced. Test the -Wfoo case, and
417418
dnl set the -Wno-foo case if it works.
418419
AX_CHECK_COMPILE_FLAG([-Wunused-parameter], [NOWARN_CXXFLAGS="$NOWARN_CXXFLAGS -Wno-unused-parameter"], [], [$CXXFLAG_WERROR])
419-
AX_CHECK_COMPILE_FLAG([-Wself-assign], [NOWARN_CXXFLAGS="$NOWARN_CXXFLAGS -Wno-self-assign"], [], [$CXXFLAG_WERROR])
420420

421421
dnl Don't allow extended (non-ASCII) symbols in identifiers. This is easier for code review.
422422
AX_CHECK_COMPILE_FLAG([-fno-extended-identifiers], [CORE_CXXFLAGS="$CORE_CXXFLAGS -fno-extended-identifiers"], [], [$CXXFLAG_WERROR])

src/test/fuzz/muhash.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,19 @@ FUZZ_TARGET(muhash)
4343
},
4444
[&] {
4545
// Test that dividing a MuHash by itself brings it back to it's initial state
46+
47+
// See note about clang + self-assignment in test/uint256_tests.cpp
48+
#if defined(__clang__)
49+
# pragma clang diagnostic push
50+
# pragma clang diagnostic ignored "-Wself-assign-overloaded"
51+
#endif
52+
4653
muhash /= muhash;
54+
55+
#if defined(__clang__)
56+
# pragma clang diagnostic pop
57+
#endif
58+
4759
muhash.Finalize(out);
4860
out2 = uint256S(initial_state_hash);
4961
},

src/test/uint256_tests.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,22 @@ BOOST_AUTO_TEST_CASE( conversion )
267267

268268
BOOST_AUTO_TEST_CASE( operator_with_self )
269269
{
270+
271+
/* Clang 16 and earlier detects v -= v and v /= v as self-assignments
272+
to 0 and 1 respectively.
273+
See: https://github.com/llvm/llvm-project/issues/42469
274+
and the fix in commit c5302325b2a62d77cf13dd16cd5c19141862fed0 .
275+
276+
This makes some sense for arithmetic classes, but could be considered a bug
277+
elsewhere. Disable the warning here so that the code can be tested, but the
278+
warning should remain on as there will likely always be a better way to
279+
express this.
280+
*/
281+
282+
#if defined(__clang__)
283+
# pragma clang diagnostic push
284+
# pragma clang diagnostic ignored "-Wself-assign-overloaded"
285+
#endif
270286
arith_uint256 v = UintToArith256(uint256S("02"));
271287
v *= v;
272288
BOOST_CHECK(v == UintToArith256(uint256S("04")));
@@ -276,6 +292,9 @@ BOOST_AUTO_TEST_CASE( operator_with_self )
276292
BOOST_CHECK(v == UintToArith256(uint256S("02")));
277293
v -= v;
278294
BOOST_CHECK(v == UintToArith256(uint256S("0")));
295+
#if defined(__clang__)
296+
# pragma clang diagnostic pop
297+
#endif
279298
}
280299

281300
BOOST_AUTO_TEST_CASE(parse)

0 commit comments

Comments
 (0)