mruby-bigint: add safety margin to karatsuba scratch allocation

prevents buffer overrun in karatsuba multiplication scratch space due to
rounding errors in recursive partitioning. empirically determined 8-limb
margin fixes valgrind-detected overrun with large exponentiations.

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
Yukihiro "Matz" Matsumoto
2025-11-15 12:11:31 +09:00
parent 8e50a45f3e
commit 9133124bef
+2
View File
@@ -1104,6 +1104,8 @@ mpz_mul(mpz_ctx_t *ctx, mpz_t *ww, mpz_t *u, mpz_t *v)
mpz_realloc(ctx, ww, result_size);
size_t scratch_size = karatsuba_scratch_size(u->sz, v->sz);
/* Add safety margin to account for rounding in recursive partitioning */
scratch_size += 8;
size_t pool_state = pool_save(ctx);
mp_limb *scratch = NULL;