Fix an overflow bug in SignedAmount to_string/fmt
This commit is contained in:
parent
abc70781e7
commit
ab6e20c87e
|
@ -668,7 +668,11 @@ impl SignedAmount {
|
||||||
///
|
///
|
||||||
/// Does not include the denomination.
|
/// Does not include the denomination.
|
||||||
pub fn fmt_value_in(&self, f: &mut fmt::Write, denom: Denomination) -> fmt::Result {
|
pub fn fmt_value_in(&self, f: &mut fmt::Write, denom: Denomination) -> fmt::Result {
|
||||||
fmt_satoshi_in(self.as_sat().abs() as u64, self.is_negative(), f, denom)
|
let sats = self.as_sat().checked_abs().map(|a: i64| a as u64).unwrap_or_else(|| {
|
||||||
|
// We could also hard code this into `9223372036854775808`
|
||||||
|
u64::max_value() - self.as_sat() as u64 +1
|
||||||
|
});
|
||||||
|
fmt_satoshi_in(sats, self.is_negative(), f, denom)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get a string number of this [SignedAmount] in the given denomination.
|
/// Get a string number of this [SignedAmount] in the given denomination.
|
||||||
|
@ -717,6 +721,13 @@ impl SignedAmount {
|
||||||
self.0.is_negative()
|
self.0.is_negative()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// Get the absolute value of this [SignedAmount].
|
||||||
|
/// Returns [None] if overflow occurred. (`self == min_value()`)
|
||||||
|
pub fn checked_abs(self) -> Option<SignedAmount> {
|
||||||
|
self.0.checked_abs().map(SignedAmount)
|
||||||
|
}
|
||||||
|
|
||||||
/// Checked addition.
|
/// Checked addition.
|
||||||
/// Returns [None] if overflow occurred.
|
/// Returns [None] if overflow occurred.
|
||||||
pub fn checked_add(self, rhs: SignedAmount) -> Option<SignedAmount> {
|
pub fn checked_add(self, rhs: SignedAmount) -> Option<SignedAmount> {
|
||||||
|
|
Loading…
Reference in New Issue