From bac0ea22af6cfee692249808eb9b5bafc07f949c Mon Sep 17 00:00:00 2001 From: Stephen Waits Date: Sun, 8 Nov 2026 10:09:35 -0700 Subject: [PATCH] fix(core): Money::checked_add and checked_sub return an error on overflow, never panic Both were named checked but only checked the currency; the amounts went through raw Decimal + and -, which panic past Decimal's range. A sum or difference too large for exact arithmetic is now refused with TooLargeToCompute, as everywhere else in the engine: never rounded, wrapped or crashed. Currency mismatch still reports as a mismatch. Test: checked_arithmetic_refuses_overflow_instead_of_panicking (MAX+MAX, MIN+MIN, MIN-MAX, MAX-MIN refused; MAX+MIN and MAX-MAX exact). Co-Authored-By: Claude Sonnet 5.5 --- crates/ledger-core/src/money.rs | 50 ++++++++++++++++++++++++++++++--- 1 file changed, 46 insertions(+), 4 deletions(-) diff --git a/crates/ledger-core/src/money.rs b/crates/ledger-core/src/money.rs index dba648ab..2ed53c1c 100644 --- a/crates/ledger-core/src/money.rs +++ b/crates/ledger-core/src/money.rs @@ -129,16 +129,27 @@ impl Money { Ok(()) } - /// Add two money values. Fails on currency mismatch. + /// Add two money values. Fails on currency mismatch, and with + /// `TooLargeToCompute` when the sum is past exact arithmetic (never a + /// panic, never rounded). pub fn checked_add(self, rhs: Money) -> Result { self.check_currency(&rhs)?; - Ok(Money::new(self.amount + rhs.amount, self.currency)) + let amount = self + .amount + .checked_add(rhs.amount) + .ok_or_else(|| LedgerError::TooLargeToCompute(format!("{} plus {}", self, rhs)))?; + Ok(Money::new(amount, self.currency)) } - /// Subtract two money values. Fails on currency mismatch. + /// Subtract two money values. Fails on currency mismatch, and with + /// `TooLargeToCompute` when the difference is past exact arithmetic. pub fn checked_sub(self, rhs: Money) -> Result { self.check_currency(&rhs)?; - Ok(Money::new(self.amount - rhs.amount, self.currency)) + let amount = self + .amount + .checked_sub(rhs.amount) + .ok_or_else(|| LedgerError::TooLargeToCompute(format!("{} minus {}", self, rhs)))?; + Ok(Money::new(amount, self.currency)) } } @@ -247,6 +258,37 @@ mod tests { ); } + #[test] + fn checked_arithmetic_refuses_overflow_instead_of_panicking() { + let max = usd(Decimal::MAX); + let min = usd(Decimal::MIN); + assert!(matches!( + max.checked_add(max), + Err(LedgerError::TooLargeToCompute(_)) + )); + assert!(matches!( + min.checked_add(min), + Err(LedgerError::TooLargeToCompute(_)) + )); + assert!(matches!( + min.checked_sub(max), + Err(LedgerError::TooLargeToCompute(_)) + )); + assert!(matches!( + max.checked_sub(min), + Err(LedgerError::TooLargeToCompute(_)) + )); + // At the edge, exact results still work. + assert_eq!(max.checked_add(min).unwrap().amount, Decimal::ZERO); + assert_eq!(max.checked_sub(max).unwrap().amount, Decimal::ZERO); + // Currency mismatch still reports as a mismatch. + let eur = Money::new(Decimal::ONE, Currency::new("EUR").unwrap()); + assert!(matches!( + max.checked_add(eur), + Err(LedgerError::CurrencyMismatch { .. }) + )); + } + /// ISO 4217 list one (docs/spec/sources/iso-4217.md): one code from /// each exponent class, and the codes with no minor unit. #[test]