Skip to content

Commit 47b99ab

Browse files
committed
Merge #20406: util: Avoid invalid integer negation in FormatMoney and ValueFromAmount
1f05dbd util: Avoid invalid integer negation in ValueFromAmount: make ValueFromAmount(const CAmount& n) well-defined also when n is std::numeric_limits<CAmount>::min() (practicalswift) 7cc75c9 util: Avoid invalid integer negation in FormatMoney: make FormatMoney(const CAmount& n) well-defined also when n is std::numeric_limits<CAmount>::min() (practicalswift) Pull request description: Avoid invalid integer negation in `FormatMoney` and `ValueFromAmount`. Fixes #20402. Before this patch: ``` $ CC=clang CXX=clang++ ./configure --with-sanitizers=undefined $ make -C src/ test/test_bitcoin $ src/test/test_bitcoin -t rpc_tests/rpc_format_monetary_values -t util_tests/util_FormatMoney core_write.cpp:21:29: runtime error: negation of -9223372036854775808 cannot be represented in type 'CAmount' (aka 'long'); cast to an unsigned type to negate this value to itself SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior core_write.cpp:21:29 in test/rpc_tests.cpp(186): error: in "rpc_tests/rpc_format_monetary_values": check ValueFromAmount(std::numeric_limits<CAmount>::min()).write() == "-92233720368.54775808" has failed [--92233720368.-54775808 != -92233720368.54775808] util/moneystr.cpp:16:34: runtime error: negation of -9223372036854775808 cannot be represented in type 'CAmount' (aka 'long'); cast to an unsigned type to negate this value to itself SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior util/moneystr.cpp:16:34 in test/util_tests.cpp(1188): error: in "util_tests/util_FormatMoney": check FormatMoney(std::numeric_limits<CAmount>::min()) == "-92233720368.54775808" has failed [--92233720368.-54775808 != -92233720368.54775808] ``` After this patch: ``` $ CC=clang CXX=clang++ ./configure --with-sanitizers=undefined $ make -C src/ test/test_bitcoin $ src/test/test_bitcoin -t rpc_tests/rpc_format_monetary_values -t util_tests/util_FormatMoney ``` ACKs for top commit: laanwj: re-ACK 1f05dbd Tree-SHA512: 5aaeb8e2178f1597921f53c12bdfc2f3d5993d10c41658dcd25943e54e8cc2116a411bc71d928f890b33bc0b3761a8ee4449b0532bce41125b6c60692808c8c3
2 parents cabe637 + 1f05dbd commit 47b99ab

File tree

8 files changed

+45
-29
lines changed

8 files changed

+45
-29
lines changed

src/core_io.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ std::vector<unsigned char> ParseHexUV(const UniValue& v, const std::string& strN
4040
int ParseSighashString(const UniValue& sighash);
4141

4242
// core_write.cpp
43-
UniValue ValueFromAmount(const CAmount& amount);
43+
UniValue ValueFromAmount(const CAmount amount);
4444
std::string FormatScript(const CScript& script);
4545
std::string EncodeHexTx(const CTransaction& tx, const int serializeFlags = 0);
4646
std::string SighashToStr(unsigned char sighash_type);

src/core_write.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,20 @@
1414
#include <undo.h>
1515
#include <univalue.h>
1616
#include <util/check.h>
17-
#include <util/system.h>
1817
#include <util/strencodings.h>
18+
#include <util/system.h>
1919

20-
UniValue ValueFromAmount(const CAmount& amount)
20+
UniValue ValueFromAmount(const CAmount amount)
2121
{
22-
bool sign = amount < 0;
23-
int64_t n_abs = (sign ? -amount : amount);
24-
int64_t quotient = n_abs / COIN;
25-
int64_t remainder = n_abs % COIN;
22+
static_assert(COIN > 1);
23+
int64_t quotient = amount / COIN;
24+
int64_t remainder = amount % COIN;
25+
if (amount < 0) {
26+
quotient = -quotient;
27+
remainder = -remainder;
28+
}
2629
return UniValue(UniValue::VNUM,
27-
strprintf("%s%d.%08d", sign ? "-" : "", quotient, remainder));
30+
strprintf("%s%d.%08d", amount < 0 ? "-" : "", quotient, remainder));
2831
}
2932

3033
std::string FormatScript(const CScript& script)

src/test/fuzz/integer.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,7 @@ FUZZ_TARGET_INIT(integer, initialize_integer)
8484
(void)DecompressAmount(u64);
8585
(void)FormatISO8601Date(i64);
8686
(void)FormatISO8601DateTime(i64);
87-
// FormatMoney(i) not defined when i == std::numeric_limits<int64_t>::min()
88-
if (i64 != std::numeric_limits<int64_t>::min()) {
87+
{
8988
int64_t parsed_money;
9089
if (ParseMoney(FormatMoney(i64), parsed_money)) {
9190
assert(parsed_money == i64);
@@ -132,8 +131,7 @@ FUZZ_TARGET_INIT(integer, initialize_integer)
132131
(void)SipHashUint256Extra(u64, u64, u256, u32);
133132
(void)ToLower(ch);
134133
(void)ToUpper(ch);
135-
// ValueFromAmount(i) not defined when i == std::numeric_limits<int64_t>::min()
136-
if (i64 != std::numeric_limits<int64_t>::min()) {
134+
{
137135
int64_t parsed_money;
138136
if (ParseMoney(ValueFromAmount(i64).getValStr(), parsed_money)) {
139137
assert(parsed_money == i64);

src/test/fuzz/transaction.cpp

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -100,16 +100,7 @@ FUZZ_TARGET_INIT(transaction, initialize_transaction)
100100
(void)IsWitnessStandard(tx, coins_view_cache);
101101

102102
UniValue u(UniValue::VOBJ);
103-
// ValueFromAmount(i) not defined when i == std::numeric_limits<int64_t>::min()
104-
bool skip_tx_to_univ = false;
105-
for (const CTxOut& txout : tx.vout) {
106-
if (txout.nValue == std::numeric_limits<int64_t>::min()) {
107-
skip_tx_to_univ = true;
108-
}
109-
}
110-
if (!skip_tx_to_univ) {
111-
TxToUniv(tx, /* hashBlock */ {}, u);
112-
static const uint256 u256_max(uint256S("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"));
113-
TxToUniv(tx, u256_max, u);
114-
}
103+
TxToUniv(tx, /* hashBlock */ {}, u);
104+
static const uint256 u256_max(uint256S("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"));
105+
TxToUniv(tx, u256_max, u);
115106
}

src/test/rpc_tests.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,16 @@ BOOST_AUTO_TEST_CASE(rpc_format_monetary_values)
174174
BOOST_CHECK_EQUAL(ValueFromAmount(COIN/1000000).write(), "0.00000100");
175175
BOOST_CHECK_EQUAL(ValueFromAmount(COIN/10000000).write(), "0.00000010");
176176
BOOST_CHECK_EQUAL(ValueFromAmount(COIN/100000000).write(), "0.00000001");
177+
178+
BOOST_CHECK_EQUAL(ValueFromAmount(std::numeric_limits<CAmount>::max()).write(), "92233720368.54775807");
179+
BOOST_CHECK_EQUAL(ValueFromAmount(std::numeric_limits<CAmount>::max() - 1).write(), "92233720368.54775806");
180+
BOOST_CHECK_EQUAL(ValueFromAmount(std::numeric_limits<CAmount>::max() - 2).write(), "92233720368.54775805");
181+
BOOST_CHECK_EQUAL(ValueFromAmount(std::numeric_limits<CAmount>::max() - 3).write(), "92233720368.54775804");
182+
// ...
183+
BOOST_CHECK_EQUAL(ValueFromAmount(std::numeric_limits<CAmount>::min() + 3).write(), "-92233720368.54775805");
184+
BOOST_CHECK_EQUAL(ValueFromAmount(std::numeric_limits<CAmount>::min() + 2).write(), "-92233720368.54775806");
185+
BOOST_CHECK_EQUAL(ValueFromAmount(std::numeric_limits<CAmount>::min() + 1).write(), "-92233720368.54775807");
186+
BOOST_CHECK_EQUAL(ValueFromAmount(std::numeric_limits<CAmount>::min()).write(), "-92233720368.54775808");
177187
}
178188

179189
static UniValue ValueFromString(const std::string &str)

src/test/util_tests.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,6 +1180,16 @@ BOOST_AUTO_TEST_CASE(util_FormatMoney)
11801180
BOOST_CHECK_EQUAL(FormatMoney(COIN/1000000), "0.000001");
11811181
BOOST_CHECK_EQUAL(FormatMoney(COIN/10000000), "0.0000001");
11821182
BOOST_CHECK_EQUAL(FormatMoney(COIN/100000000), "0.00000001");
1183+
1184+
BOOST_CHECK_EQUAL(FormatMoney(std::numeric_limits<CAmount>::max()), "92233720368.54775807");
1185+
BOOST_CHECK_EQUAL(FormatMoney(std::numeric_limits<CAmount>::max() - 1), "92233720368.54775806");
1186+
BOOST_CHECK_EQUAL(FormatMoney(std::numeric_limits<CAmount>::max() - 2), "92233720368.54775805");
1187+
BOOST_CHECK_EQUAL(FormatMoney(std::numeric_limits<CAmount>::max() - 3), "92233720368.54775804");
1188+
// ...
1189+
BOOST_CHECK_EQUAL(FormatMoney(std::numeric_limits<CAmount>::min() + 3), "-92233720368.54775805");
1190+
BOOST_CHECK_EQUAL(FormatMoney(std::numeric_limits<CAmount>::min() + 2), "-92233720368.54775806");
1191+
BOOST_CHECK_EQUAL(FormatMoney(std::numeric_limits<CAmount>::min() + 1), "-92233720368.54775807");
1192+
BOOST_CHECK_EQUAL(FormatMoney(std::numeric_limits<CAmount>::min()), "-92233720368.54775808");
11831193
}
11841194

11851195
BOOST_AUTO_TEST_CASE(util_ParseMoney)

src/util/moneystr.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,17 @@
99
#include <util/strencodings.h>
1010
#include <util/string.h>
1111

12-
std::string FormatMoney(const CAmount& n)
12+
std::string FormatMoney(const CAmount n)
1313
{
1414
// Note: not using straight sprintf here because we do NOT want
1515
// localized number formatting.
16-
int64_t n_abs = (n > 0 ? n : -n);
17-
int64_t quotient = n_abs/COIN;
18-
int64_t remainder = n_abs%COIN;
16+
static_assert(COIN > 1);
17+
int64_t quotient = n / COIN;
18+
int64_t remainder = n % COIN;
19+
if (n < 0) {
20+
quotient = -quotient;
21+
remainder = -remainder;
22+
}
1923
std::string str = strprintf("%d.%08d", quotient, remainder);
2024

2125
// Right-trim excess zeros before the decimal point:

src/util/moneystr.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
/* Do not use these functions to represent or parse monetary amounts to or from
1818
* JSON but use AmountFromValue and ValueFromAmount for that.
1919
*/
20-
std::string FormatMoney(const CAmount& n);
20+
std::string FormatMoney(const CAmount n);
2121
/** Parse an amount denoted in full coins. E.g. "0.0034" supplied on the command line. **/
2222
[[nodiscard]] bool ParseMoney(const std::string& str, CAmount& nRet);
2323

0 commit comments

Comments
 (0)