From 69586805e7ab9bb55c28d9c58f79d39c2228b6a5 Mon Sep 17 00:00:00 2001 From: Kyle Santiago Date: Sun, 15 Nov 2026 13:04:58 -0500 Subject: [PATCH] common: negate a negative s64 amount without signed overflow --- channeld/full_channel.c | 4 ++-- common/amount.c | 4 ++-- common/test/run-amount.c | 11 +++++++++++ wallet/wallet.c | 2 +- 4 files changed, 16 insertions(+), 5 deletions(-) diff --git a/channeld/full_channel.c b/channeld/full_channel.c index 4b726620d161..39cbd13c81b8 100644 --- a/channeld/full_channel.c +++ b/channeld/full_channel.c @@ -456,14 +456,14 @@ static bool get_room_above_reserve(const struct channel *channel, struct amount_msat owed = view->owed[side]; /* `lowest_splice_amnt` will always be negative or 0 */ - if (amount_msat_less_eq_sat(owed, amount_sat(-view->lowest_splice_amnt[side]))) { + if (amount_msat_less_eq_sat(owed, amount_sat(-(u64)view->lowest_splice_amnt[side]))) { status_debug("Relative splice balance invalid"); return false; } /* `lowest_splice_amnt` is a relative amount */ if (!amount_msat_sub_sat(&owed, owed, - amount_sat(-view->lowest_splice_amnt[side]))) { + amount_sat(-(u64)view->lowest_splice_amnt[side]))) { status_debug("Owed amount should not wrap around from splice"); return false; } diff --git a/common/amount.c b/common/amount.c index bccff3f882e4..4c87fecc537c 100644 --- a/common/amount.c +++ b/common/amount.c @@ -402,7 +402,7 @@ WARN_UNUSED_RESULT bool amount_msat_add_sat_s64(struct amount_msat *val, s64 b) { if (b < 0) - return amount_msat_sub_sat(val, a, amount_sat(-b)); + return amount_msat_sub_sat(val, a, amount_sat(-(u64)b)); else return amount_msat_add_sat(val, a, amount_sat(b)); } @@ -413,7 +413,7 @@ WARN_UNUSED_RESULT bool amount_sat_add_sat_s64(struct amount_sat *val, s64 b) { if (b < 0) - return amount_sat_sub(val, a, amount_sat(-b)); + return amount_sat_sub(val, a, amount_sat(-(u64)b)); else return amount_sat_add(val, a, amount_sat(b)); } diff --git a/common/test/run-amount.c b/common/test/run-amount.c index e7d5cfb82d8d..11c01743cfcc 100644 --- a/common/test/run-amount.c +++ b/common/test/run-amount.c @@ -453,6 +453,17 @@ int main(int argc, char *argv[]) assert(sat.satoshis == i); } + /* Negating INT64_MIN as a signed value overflows. */ + sat.satoshis = UINT64_MAX; + assert(amount_sat_add_sat_s64(&sat, sat, INT64_MIN)); + assert(sat.satoshis == UINT64_MAX - (1ULL << 63)); + assert(!amount_sat_add_sat_s64(&sat, AMOUNT_SAT(5), INT64_MIN)); + assert(amount_sat_add_sat_s64(&sat, AMOUNT_SAT(5), -5)); + assert(amount_sat_is_zero(sat)); + assert(!amount_msat_add_sat_s64(&msat, AMOUNT_MSAT(UINT64_MAX), INT64_MIN)); + assert(amount_msat_add_sat_s64(&msat, AMOUNT_MSAT(5000), -5)); + assert(amount_msat_is_zero(msat)); + test_amount_with_fee(); test_amount_div(); common_shutdown(); diff --git a/wallet/wallet.c b/wallet/wallet.c index 99636f7cbd47..17191319c4e1 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -7258,7 +7258,7 @@ static void db_col_credit_or_debit(struct db_stmt *stmt, s64 amount = db_col_s64(stmt, colname); if (amount < 0) { *credit = AMOUNT_MSAT(0); - *debit = amount_msat(-amount); + *debit = amount_msat(-(u64)amount); } else { *credit = amount_msat(amount); *debit = AMOUNT_MSAT(0);