amount: Can add sat convenience method
What changed, and why it matters
This commit adds a small helper function that checks whether a signed satoshi value can safely be added to (or subtracted from) a millisatoshi amount without going below zero. It is purely a convenience wrapper around an existing safe arithmetic function and does not change any behavior or fix any bug on its own.
No security action required. Treat as routine code cleanup / API addition.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch introduces amount_msat_can_add_sat_s64() in common/amount.c/h. It wraps amount_msat_add_sat_s64() and returns true if the operation succeeds (i.e., does not underflow). The function is marked WARN_UNUSED_RESULT. No call sites are added or modified, and no existing logic is changed.
Changed components
common/amount.ccommon/amount.hInspect captured patch +10 / −0
diff --git a/common/amount.c b/common/amount.c
index d25448e9..bccff3f8 100644
--- a/common/amount.c
+++ b/common/amount.c
@@ -418,6 +418,13 @@ WARN_UNUSED_RESULT bool amount_sat_add_sat_s64(struct amount_sat *val,
return amount_sat_add(val, a, amount_sat(b));
}
+bool amount_msat_can_add_sat_s64(struct amount_msat a, s64 b)
+{
+ struct amount_msat val;
+ /* This fails if the result goes below 0 */
+ return amount_msat_add_sat_s64(&val, a, b);
+}
+
bool amount_sat_eq(struct amount_sat a, struct amount_sat b)
{
return a.satoshis == b.satoshis;
diff --git a/common/amount.h b/common/amount.h
index 2c3901c4..9cfc262c 100644
--- a/common/amount.h
+++ b/common/amount.h
@@ -101,6 +101,9 @@ WARN_UNUSED_RESULT bool amount_sat_add_sat_s64(struct amount_sat *val,
struct amount_sat a,
s64 b);
+WARN_UNUSED_RESULT bool amount_msat_can_add_sat_s64(struct amount_msat a,
+ s64 b);
+
/* a += b */
WARN_UNUSED_RESULT bool amount_msat_accumulate(struct amount_msat *a,
struct amount_msat b);
Why this scored 15/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.