mruby-bigint: fix carry placement in uadd()

The final carry was stored at z->p[y->sz], but when x is larger
than y, this index falls within the already-computed result and
corrupts it. Store at z->p[i] instead, which correctly points to
max(x->sz, y->sz) after all loops complete.

This bug caused incorrect results when adding a small number to
an all-ones number with 1124+ limbs (35968+ bits).

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
Yukihiro "Matz" Matsumoto
2026-01-10 08:30:18 +09:00
parent 512fffdac8
commit 9d04c74ed8
+2 -2
View File
@@ -432,8 +432,8 @@ uadd(mpz_t *z, mpz_t *x, mpz_t *y)
c >>= DIG_SIZE;
}
/* Store final carry */
z->p[y->sz] = (mp_limb)c;
/* Store final carry at correct position (after all limbs) */
z->p[i] = (mp_limb)c;
}
/* z = y - x, ignoring sign */