mirror of
https://github.com/ElementsProject/elements.git
synced 2026-08-14 12:43:40 +02: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
|
|
@ -14,17 +14,20 @@
|
|||
#include <undo.h>
|
||||
#include <univalue.h>
|
||||
#include <util/check.h>
|
||||
#include <util/system.h>
|
||||
#include <util/strencodings.h>
|
||||
#include <util/system.h>
|
||||
|
||||
UniValue ValueFromAmount(const CAmount& amount)
|
||||
UniValue ValueFromAmount(const CAmount amount)
|
||||
{
|
||||
bool sign = amount < 0;
|
||||
int64_t n_abs = (sign ? -amount : amount);
|
||||
int64_t quotient = n_abs / COIN;
|
||||
int64_t remainder = n_abs % COIN;
|
||||
static_assert(COIN > 1);
|
||||
int64_t quotient = amount / COIN;
|
||||
int64_t remainder = amount % COIN;
|
||||
if (amount < 0) {
|
||||
quotient = -quotient;
|
||||
remainder = -remainder;
|
||||
}
|
||||
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)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue