Fix an overflow bug in SignedAmount to_string/fmt

This commit is contained in:
Elichai Turkel 2020-01-21 19:42:44 +02:00
parent abc70781e7
commit ab6e20c87e
No known key found for this signature in database
GPG Key ID: 9383CDE9E8E66A7F
1 changed files with 12 additions and 1 deletions

View File

@ -668,7 +668,11 @@ impl SignedAmount {
///
/// Does not include the denomination.
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.
@ -717,6 +721,13 @@ impl SignedAmount {
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.
/// Returns [None] if overflow occurred.
pub fn checked_add(self, rhs: SignedAmount) -> Option<SignedAmount> {