Skip to content

Commit 7cc75c9

Browse files
util: Avoid invalid integer negation in FormatMoney: make FormatMoney(const CAmount& n) well-defined also when n is std::numeric_limits<CAmount>::min()
1 parent b9f41df commit 7cc75c9

File tree

4 files changed

+20
-7
lines changed

4 files changed

+20
-7
lines changed

src/test/fuzz/integer.cpp

Lines changed: 1 addition & 2 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);

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)