mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-01-11 20:32:35 -03:00
util: Avoid invalid integer negation in ValueFromAmount: make ValueFromAmount(const CAmount& n) well-defined also when n is std::numeric_limits<CAmount>::min()
This commit is contained in:
parent
7cc75c9ba3
commit
1f05dbd06d
5 changed files with 25 additions and 22 deletions
|
@ -40,7 +40,7 @@ std::vector<unsigned char> ParseHexUV(const UniValue& v, const std::string& strN
|
||||||
int ParseSighashString(const UniValue& sighash);
|
int ParseSighashString(const UniValue& sighash);
|
||||||
|
|
||||||
// core_write.cpp
|
// core_write.cpp
|
||||||
UniValue ValueFromAmount(const CAmount& amount);
|
UniValue ValueFromAmount(const CAmount amount);
|
||||||
std::string FormatScript(const CScript& script);
|
std::string FormatScript(const CScript& script);
|
||||||
std::string EncodeHexTx(const CTransaction& tx, const int serializeFlags = 0);
|
std::string EncodeHexTx(const CTransaction& tx, const int serializeFlags = 0);
|
||||||
std::string SighashToStr(unsigned char sighash_type);
|
std::string SighashToStr(unsigned char sighash_type);
|
||||||
|
|
|
@ -14,17 +14,20 @@
|
||||||
#include <undo.h>
|
#include <undo.h>
|
||||||
#include <univalue.h>
|
#include <univalue.h>
|
||||||
#include <util/check.h>
|
#include <util/check.h>
|
||||||
#include <util/system.h>
|
|
||||||
#include <util/strencodings.h>
|
#include <util/strencodings.h>
|
||||||
|
#include <util/system.h>
|
||||||
|
|
||||||
UniValue ValueFromAmount(const CAmount& amount)
|
UniValue ValueFromAmount(const CAmount amount)
|
||||||
{
|
{
|
||||||
bool sign = amount < 0;
|
static_assert(COIN > 1);
|
||||||
int64_t n_abs = (sign ? -amount : amount);
|
int64_t quotient = amount / COIN;
|
||||||
int64_t quotient = n_abs / COIN;
|
int64_t remainder = amount % COIN;
|
||||||
int64_t remainder = n_abs % COIN;
|
if (amount < 0) {
|
||||||
|
quotient = -quotient;
|
||||||
|
remainder = -remainder;
|
||||||
|
}
|
||||||
return UniValue(UniValue::VNUM,
|
return UniValue(UniValue::VNUM,
|
||||||
strprintf("%s%d.%08d", sign ? "-" : "", quotient, remainder));
|
strprintf("%s%d.%08d", amount < 0 ? "-" : "", quotient, remainder));
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string FormatScript(const CScript& script)
|
std::string FormatScript(const CScript& script)
|
||||||
|
|
|
@ -131,8 +131,7 @@ FUZZ_TARGET_INIT(integer, initialize_integer)
|
||||||
(void)SipHashUint256Extra(u64, u64, u256, u32);
|
(void)SipHashUint256Extra(u64, u64, u256, u32);
|
||||||
(void)ToLower(ch);
|
(void)ToLower(ch);
|
||||||
(void)ToUpper(ch);
|
(void)ToUpper(ch);
|
||||||
// ValueFromAmount(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(ValueFromAmount(i64).getValStr(), parsed_money)) {
|
if (ParseMoney(ValueFromAmount(i64).getValStr(), parsed_money)) {
|
||||||
assert(parsed_money == i64);
|
assert(parsed_money == i64);
|
||||||
|
|
|
@ -100,16 +100,7 @@ FUZZ_TARGET_INIT(transaction, initialize_transaction)
|
||||||
(void)IsWitnessStandard(tx, coins_view_cache);
|
(void)IsWitnessStandard(tx, coins_view_cache);
|
||||||
|
|
||||||
UniValue u(UniValue::VOBJ);
|
UniValue u(UniValue::VOBJ);
|
||||||
// ValueFromAmount(i) not defined when i == std::numeric_limits<int64_t>::min()
|
|
||||||
bool skip_tx_to_univ = false;
|
|
||||||
for (const CTxOut& txout : tx.vout) {
|
|
||||||
if (txout.nValue == std::numeric_limits<int64_t>::min()) {
|
|
||||||
skip_tx_to_univ = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!skip_tx_to_univ) {
|
|
||||||
TxToUniv(tx, /* hashBlock */ {}, u);
|
TxToUniv(tx, /* hashBlock */ {}, u);
|
||||||
static const uint256 u256_max(uint256S("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"));
|
static const uint256 u256_max(uint256S("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"));
|
||||||
TxToUniv(tx, u256_max, u);
|
TxToUniv(tx, u256_max, u);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -174,6 +174,16 @@ BOOST_AUTO_TEST_CASE(rpc_format_monetary_values)
|
||||||
BOOST_CHECK_EQUAL(ValueFromAmount(COIN/1000000).write(), "0.00000100");
|
BOOST_CHECK_EQUAL(ValueFromAmount(COIN/1000000).write(), "0.00000100");
|
||||||
BOOST_CHECK_EQUAL(ValueFromAmount(COIN/10000000).write(), "0.00000010");
|
BOOST_CHECK_EQUAL(ValueFromAmount(COIN/10000000).write(), "0.00000010");
|
||||||
BOOST_CHECK_EQUAL(ValueFromAmount(COIN/100000000).write(), "0.00000001");
|
BOOST_CHECK_EQUAL(ValueFromAmount(COIN/100000000).write(), "0.00000001");
|
||||||
|
|
||||||
|
BOOST_CHECK_EQUAL(ValueFromAmount(std::numeric_limits<CAmount>::max()).write(), "92233720368.54775807");
|
||||||
|
BOOST_CHECK_EQUAL(ValueFromAmount(std::numeric_limits<CAmount>::max() - 1).write(), "92233720368.54775806");
|
||||||
|
BOOST_CHECK_EQUAL(ValueFromAmount(std::numeric_limits<CAmount>::max() - 2).write(), "92233720368.54775805");
|
||||||
|
BOOST_CHECK_EQUAL(ValueFromAmount(std::numeric_limits<CAmount>::max() - 3).write(), "92233720368.54775804");
|
||||||
|
// ...
|
||||||
|
BOOST_CHECK_EQUAL(ValueFromAmount(std::numeric_limits<CAmount>::min() + 3).write(), "-92233720368.54775805");
|
||||||
|
BOOST_CHECK_EQUAL(ValueFromAmount(std::numeric_limits<CAmount>::min() + 2).write(), "-92233720368.54775806");
|
||||||
|
BOOST_CHECK_EQUAL(ValueFromAmount(std::numeric_limits<CAmount>::min() + 1).write(), "-92233720368.54775807");
|
||||||
|
BOOST_CHECK_EQUAL(ValueFromAmount(std::numeric_limits<CAmount>::min()).write(), "-92233720368.54775808");
|
||||||
}
|
}
|
||||||
|
|
||||||
static UniValue ValueFromString(const std::string &str)
|
static UniValue ValueFromString(const std::string &str)
|
||||||
|
|
Loading…
Reference in a new issue