Skip to content

common: negate a negative s64 amount without signed overflow - #112

Merged
kwsantiago merged 1 commit into
blake2b-unifiedfrom
amount-s64-min
Nov 16, 2026
Merged

kwsantiago merged 1 commit into
blake2b-unifiedfrom
amount-s64-min

Conversation

@kwsantiago

Copy link
Copy Markdown

Summary

amount_msat_add_sat_s64() and amount_sat_add_sat_s64() negated a negative s64 with -b, which is signed overflow when b is INT64_MIN. A peer can send that value as a splice contribution, which reaches these helpers in channeld's check_balances() and calc_balance(). Normal builds wrapped to 2^63 and failed the subtraction, but a UBSan build aborts channeld.

  • Negate in unsigned arithmetic, -(u64)b, which is exact for every negative s64, including INT64_MIN. The splice then fails cleanly as before.
  • The same pattern in get_room_above_reserve() and in reading credit and debit columns from the database gets the same change. Neither takes a value a peer controls.

Changelog-None

Test plan

  • New common/test/run-amount.c cases for INT64_MIN and ordinary negatives, in both helpers.
  • Under UBSan the new test aborts on the old code with "negation of -9223372036854775808 cannot be represented in type 's64'" and passes with the fix.
  • All other unit tests pass under UBSan, apart from 13 that fail in ccan's ptrint.h (a null pointer offset) on code this change does not touch.

@kwsantiago
kwsantiago merged commit 48dcc47 into blake2b-unified Nov 16, 2026
46 checks passed
@kwsantiago
kwsantiago deleted the amount-s64-min branch November 16, 2026 00:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant