diff options
author | practicalswift <practicalswift@users.noreply.github.com> | 2020-11-16 15:42:36 +0000 |
---|---|---|
committer | practicalswift <practicalswift@users.noreply.github.com> | 2021-03-02 16:05:28 +0000 |
commit | 7cc75c9ba38e516067e5a4ab84311c62ddddced7 (patch) | |
tree | f0f5f34a5fdb9d4b1f53955cb2ae6044fa426575 /src/util/moneystr.cpp | |
parent | b9f41df1ead4b6a83a51fc41966b111c8459c313 (diff) |
util: Avoid invalid integer negation in FormatMoney: make FormatMoney(const CAmount& n) well-defined also when n is std::numeric_limits<CAmount>::min()
Diffstat (limited to 'src/util/moneystr.cpp')
-rw-r--r-- | src/util/moneystr.cpp | 12 |
1 files changed, 8 insertions, 4 deletions
diff --git a/src/util/moneystr.cpp b/src/util/moneystr.cpp index 1bc8d02eab..3f9ce7dce4 100644 --- a/src/util/moneystr.cpp +++ b/src/util/moneystr.cpp @@ -9,13 +9,17 @@ #include <util/strencodings.h> #include <util/string.h> -std::string FormatMoney(const CAmount& n) +std::string FormatMoney(const CAmount n) { // Note: not using straight sprintf here because we do NOT want // localized number formatting. - int64_t n_abs = (n > 0 ? n : -n); - int64_t quotient = n_abs/COIN; - int64_t remainder = n_abs%COIN; + static_assert(COIN > 1); + int64_t quotient = n / COIN; + int64_t remainder = n % COIN; + if (n < 0) { + quotient = -quotient; + remainder = -remainder; + } std::string str = strprintf("%d.%08d", quotient, remainder); // Right-trim excess zeros before the decimal point: |