Avoid invalid integer negation in FormatMoney
and ValueFromAmount
.
Fixes #20402.
Before this patch:
0$ CC=clang CXX=clang++ ./configure --with-sanitizers=undefined
1$ make -C src/ test/test_bitcoin
2$ src/test/test_bitcoin -t rpc_tests/rpc_format_monetary_values -t util_tests/util_FormatMoney
3core_write.cpp:21:29: runtime error: negation of -9223372036854775808 cannot be represented in type 'CAmount'
4 (aka 'long'); cast to an unsigned type to negate this value to itself
5SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior core_write.cpp:21:29 in
6test/rpc_tests.cpp(186): error: in "rpc_tests/rpc_format_monetary_values":
7 check ValueFromAmount(std::numeric_limits<CAmount>::min()).write() == "-92233720368.54775808" has failed
8 [--92233720368.-54775808 != -92233720368.54775808]
9util/moneystr.cpp:16:34: runtime error: negation of -9223372036854775808 cannot be represented in type 'CAmount'
10 (aka 'long'); cast to an unsigned type to negate this value to itself
11SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior util/moneystr.cpp:16:34 in
12test/util_tests.cpp(1188): error: in "util_tests/util_FormatMoney":
13 check FormatMoney(std::numeric_limits<CAmount>::min()) == "-92233720368.54775808" has failed
14 [--92233720368.-54775808 != -92233720368.54775808]
After this patch:
0$ CC=clang CXX=clang++ ./configure --with-sanitizers=undefined
1$ make -C src/ test/test_bitcoin
2$ src/test/test_bitcoin -t rpc_tests/rpc_format_monetary_values -t util_tests/util_FormatMoney