mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
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:
@@ -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 */
|
||||
|
||||
Reference in New Issue
Block a user