mruby-bigint: move pool handling from bint_set to mpz_move

refactored the stack-use-after-return fix to encapsulate pool memory
handling in mpz_move instead of bint_set, providing cleaner code and
automatic protection for all 22 callers of mpz_move; ref #6651

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
Yukihiro "Matz" Matsumoto
2025-10-27 08:48:04 +09:00
parent 6e89c433a8
commit 88bfa0bec1
+10 -12
View File
@@ -309,6 +309,15 @@ mpz_clear(mpz_ctx_t *ctx, mpz_t *s)
static void
mpz_move(mpz_ctx_t *ctx, mpz_t *y, mpz_t *x)
{
#if MRB_BIGINT_POOL_SIZE > 0
if (MPZ_HAS_POOL(ctx) && is_pool_memory(x, MPZ_POOL(ctx))) {
/* Source is pool memory - use deep copy instead of pointer transfer */
mpz_set(ctx, y, x);
mpz_clear(ctx, x);
return;
}
#endif
/* Normal move: transfer ownership */
mpz_clear(ctx, y);
y->sn = x->sn;
y->sz = x->sz;
@@ -2767,19 +2776,8 @@ bint_set(mpz_ctx_t *ctx, struct RBigint *b, mpz_t *x)
}
else {
RBIGINT_SET_HEAP(b);
#if MRB_BIGINT_POOL_SIZE > 0
if (MPZ_HAS_POOL(ctx) && is_pool_memory(x, MPZ_POOL(ctx))) {
/* mpz_move() cannot be used because x is in the pool. */
mpz_set(ctx, &b->as.heap, x);
mpz_clear(ctx, x);
}
else {
#endif
mpz_move(ctx, &b->as.heap, x);
}
#if MRB_BIGINT_POOL_SIZE > 0
mpz_move(ctx, &b->as.heap, x);
}
#endif
}
static struct RBigint*