rust: don't silently eat overflow errors in --release
What changed, and why it matters
This commit fixes a bug in Core Lightning's Rust code where adding or subtracting monetary amounts would silently wrap around to incorrect values in release builds, instead of failing as intended. The fix makes overflow and underflow explicitly cause an error (panic), so bad calculations can't be quietly ignored. This is a security-relevant correctness issue because silently wrong amounts could lead to incorrect balances or payments.
Review all call sites using + and - on Amount types to ensure they can handle None from checked_add/checked_sub, or confirm that panicking is acceptable. Consider whether any existing release-build behavior relied on wrapping. No immediate exploit is evident, but the fix should be included in the next release.
Security signals we found
Integer overflow/underflow in monetary amount arithmetic
Silent wrapping behavior in release builds
Potential incorrect balance or payment amount computation
Fix changes silent wrap to explicit panic via checked arithmetic
Evidence from the diff
In cln-rpc/src/primitives.rs, the amount! macro previously implemented std::ops::Add and std::ops::Sub using raw + and - on the underlying integer field. In Rust, integer overflow/underflow panics in debug builds but silently wraps in –release builds. The commit changes Add and Sub to call checked_add/checked_sub and expect() the result, so release builds also fail loudly on overflow/underflow. The checked_add/checked_sub helpers are also made pub and documented. Two tests that expected panics were failing under release because the raw operators silently wrapped instead of panicking.
Changed components
cln-rpc/src/primitives.rsAmount arithmetic (Add/Sub implementations in amount! macro)Core Lightning Rust RPC primitivesInspect captured patch +8 / −8
### cln-rpc/src/primitives.rs
@@ -189,11 +189,13 @@ macro_rules! amount {
self.$field
}
- fn checked_add(self, rhs: Self) -> Option<Self> {
+ /// Adds two amounts, returning `None` if the result overflows.
+ pub fn checked_add(self, rhs: Self) -> Option<Self> {
self.$field.checked_add(rhs.$field).map($name::$from_fn)
}
- fn checked_sub(self, rhs: Self) -> Option<Self> {
+ /// Subtracts two amounts, returning `None` if the result underflows.
+ pub fn checked_sub(self, rhs: Self) -> Option<Self> {
self.$field.checked_sub(rhs.$field).map($name::$from_fn)
}
}
@@ -202,19 +204,17 @@ macro_rules! amount {
type Output = Self;
fn add(self, rhs: Self) -> Self {
- Self {
- $field: self.$field + rhs.$field,
- }
+ self.checked_add(rhs)
+ .expect("attempt to add with overflow")
}
}
impl std::ops::Sub for $name {
type Output = Self;
fn sub(self, rhs: Self) -> Self::Output {
- Self {
- $field: self.$field - rhs.$field,
- }
+ self.checked_sub(rhs)
+ .expect("attempt to subtract with overflow")
}
}
Why this scored 61/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.