Use signed scratch arrays in the thresh ops and stack size computations
What changed, and why it matters
This commit is a small code cleanup in Ledger's Bitcoin app. It changes two local arrays from unsigned (uint16_t) to signed (int16_t) in functions that count operations and stack sizes for Bitcoin miniscript 'thresh' policies. The commit message says the old code was not actually buggy because the stored -1 values were later read back as signed -1 anyway, but using signed types directly is clearer and less error-prone.
Treat as a minor hardening/cleanup change. No urgent action is required, but downstream forks or audits should adopt the same type-correctness fix to avoid future bugs if the read path ever changes. Review related miniscript analysis code for similar signed/unsigned mismatches.
Security signals we found
Signed/unsigned type mismatch in sentinel handling
Potential latent integer interpretation bug in miniscript threshold policy analysis
Code clarity improvement around invalid (dis)satisfaction markers
Evidence from the diff
In compute_thresh_ops() and compute_thresh_stacksize() in src/common/wallet.c, local scratch arrays sats[] and next_sats[] were declared as uint16_t. Helper functions sumcheck() and maxcheck() return int16_t and use -1 to mark invalid (dis)satisfactions. Storing -1 in a uint16_t produces 0xFFFF, which when later interpreted as int16_t is -1 again, so behavior was preserved. The patch changes the arrays to int16_t so the sentinel value is stored and read consistently as -1, eliminating a latent signedness confusion and making the code easier to verify.
Changed components
src/common/wallet.ccompute_thresh_ops()compute_thresh_stacksize()miniscript threshold policy validationInspect captured patch +4 / −4
### src/common/wallet.c
@@ -2158,8 +2158,8 @@ static int16_t maxcheck(int16_t a, int16_t b) {
__attribute__((noinline)) static int compute_thresh_ops(const policy_node_thresh_t *node,
miniscript_ops_t *out,
MiniscriptContext ctx) {
- uint16_t sats[MAX_N_IN_THRESH + 1 + 1] = {0};
- uint16_t next_sats[MAX_N_IN_THRESH + 1 + 1] = {0}; // it temporarily uses an extra element
+ int16_t sats[MAX_N_IN_THRESH + 1 + 1] = {0};
+ int16_t next_sats[MAX_N_IN_THRESH + 1 + 1] = {0}; // it temporarily uses an extra element
if (node->n > MAX_N_IN_THRESH) return -1;
@@ -2197,8 +2197,8 @@ __attribute__((noinline)) static int compute_thresh_ops(const policy_node_thresh
__attribute__((noinline)) static int compute_thresh_stacksize(const policy_node_thresh_t *node,
miniscript_stacksize_t *out,
MiniscriptContext ctx) {
- uint16_t sats[MAX_N_IN_THRESH + 1 + 1] = {0};
- uint16_t next_sats[MAX_N_IN_THRESH + 1 + 1] = {0}; // it temporarily uses an extra element
+ int16_t sats[MAX_N_IN_THRESH + 1 + 1] = {0};
+ int16_t next_sats[MAX_N_IN_THRESH + 1 + 1] = {0}; // it temporarily uses an extra element
if (node->n > MAX_N_IN_THRESH) return -1;
Why this scored 18/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.