mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-02 09:46:52 -05:00
util: Avoid invalid integer negation in FormatMoney: make FormatMoney(const CAmount& n) well-defined also when n is std::numeric_limits<CAmount>::min()
This commit is contained in:
parent
b9f41df1ea
commit
7cc75c9ba3
4 changed files with 20 additions and 7 deletions
|
@ -84,8 +84,7 @@ FUZZ_TARGET_INIT(integer, initialize_integer)
|
||||||
(void)DecompressAmount(u64);
|
(void)DecompressAmount(u64);
|
||||||
(void)FormatISO8601Date(i64);
|
(void)FormatISO8601Date(i64);
|
||||||
(void)FormatISO8601DateTime(i64);
|
(void)FormatISO8601DateTime(i64);
|
||||||
// FormatMoney(i) not defined when i == std::numeric_limits<int64_t>::min()
|
{
|
||||||
if (i64 != std::numeric_limits<int64_t>::min()) {
|
|
||||||
int64_t parsed_money;
|
int64_t parsed_money;
|
||||||
if (ParseMoney(FormatMoney(i64), parsed_money)) {
|
if (ParseMoney(FormatMoney(i64), parsed_money)) {
|
||||||
assert(parsed_money == i64);
|
assert(parsed_money == i64);
|
||||||
|
|
|
@ -1180,6 +1180,16 @@ BOOST_AUTO_TEST_CASE(util_FormatMoney)
|
||||||
BOOST_CHECK_EQUAL(FormatMoney(COIN/1000000), "0.000001");
|
BOOST_CHECK_EQUAL(FormatMoney(COIN/1000000), "0.000001");
|
||||||
BOOST_CHECK_EQUAL(FormatMoney(COIN/10000000), "0.0000001");
|
BOOST_CHECK_EQUAL(FormatMoney(COIN/10000000), "0.0000001");
|
||||||
BOOST_CHECK_EQUAL(FormatMoney(COIN/100000000), "0.00000001");
|
BOOST_CHECK_EQUAL(FormatMoney(COIN/100000000), "0.00000001");
|
||||||
|
|
||||||
|
BOOST_CHECK_EQUAL(FormatMoney(std::numeric_limits<CAmount>::max()), "92233720368.54775807");
|
||||||
|
BOOST_CHECK_EQUAL(FormatMoney(std::numeric_limits<CAmount>::max() - 1), "92233720368.54775806");
|
||||||
|
BOOST_CHECK_EQUAL(FormatMoney(std::numeric_limits<CAmount>::max() - 2), "92233720368.54775805");
|
||||||
|
BOOST_CHECK_EQUAL(FormatMoney(std::numeric_limits<CAmount>::max() - 3), "92233720368.54775804");
|
||||||
|
// ...
|
||||||
|
BOOST_CHECK_EQUAL(FormatMoney(std::numeric_limits<CAmount>::min() + 3), "-92233720368.54775805");
|
||||||
|
BOOST_CHECK_EQUAL(FormatMoney(std::numeric_limits<CAmount>::min() + 2), "-92233720368.54775806");
|
||||||
|
BOOST_CHECK_EQUAL(FormatMoney(std::numeric_limits<CAmount>::min() + 1), "-92233720368.54775807");
|
||||||
|
BOOST_CHECK_EQUAL(FormatMoney(std::numeric_limits<CAmount>::min()), "-92233720368.54775808");
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOST_AUTO_TEST_CASE(util_ParseMoney)
|
BOOST_AUTO_TEST_CASE(util_ParseMoney)
|
||||||
|
|
|
@ -9,13 +9,17 @@
|
||||||
#include <util/strencodings.h>
|
#include <util/strencodings.h>
|
||||||
#include <util/string.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
|
// Note: not using straight sprintf here because we do NOT want
|
||||||
// localized number formatting.
|
// localized number formatting.
|
||||||
int64_t n_abs = (n > 0 ? n : -n);
|
static_assert(COIN > 1);
|
||||||
int64_t quotient = n_abs/COIN;
|
int64_t quotient = n / COIN;
|
||||||
int64_t remainder = n_abs%COIN;
|
int64_t remainder = n % COIN;
|
||||||
|
if (n < 0) {
|
||||||
|
quotient = -quotient;
|
||||||
|
remainder = -remainder;
|
||||||
|
}
|
||||||
std::string str = strprintf("%d.%08d", quotient, remainder);
|
std::string str = strprintf("%d.%08d", quotient, remainder);
|
||||||
|
|
||||||
// Right-trim excess zeros before the decimal point:
|
// Right-trim excess zeros before the decimal point:
|
||||||
|
|
|
@ -17,7 +17,7 @@
|
||||||
/* Do not use these functions to represent or parse monetary amounts to or from
|
/* Do not use these functions to represent or parse monetary amounts to or from
|
||||||
* JSON but use AmountFromValue and ValueFromAmount for that.
|
* JSON but use AmountFromValue and ValueFromAmount for that.
|
||||||
*/
|
*/
|
||||||
std::string FormatMoney(const CAmount& n);
|
std::string FormatMoney(const CAmount n);
|
||||||
/** Parse an amount denoted in full coins. E.g. "0.0034" supplied on the command line. **/
|
/** Parse an amount denoted in full coins. E.g. "0.0034" supplied on the command line. **/
|
||||||
[[nodiscard]] bool ParseMoney(const std::string& str, CAmount& nRet);
|
[[nodiscard]] bool ParseMoney(const std::string& str, CAmount& nRet);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue