mruby-bigint: add missing trim in shift functions

Add trim() calls after mpz_set/mpz_move in shift operations where
actual bit manipulation is skipped:

- mpz_mul_2exp when e==0 (no shift needed)
- mpz_mul_2exp when bs==0 (limb-only shift)
- mpz_div_2exp when e==0 (no shift needed)
- mpz_div_2exp when bs==0 (limb-only shift)

This prevents inflated sz values from propagating through operations,
complementing the earlier fix to urshift/ulshift when n==0.

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
Yukihiro "Matz" Matsumoto
2026-01-12 22:27:04 +09:00
parent f2f385f572
commit 61aa2234d8
+6 -1
View File
@@ -2812,8 +2812,10 @@ mpz_get_int(mpz_t *y, mrb_int *v)
static void
mpz_mul_2exp(mpz_ctx_t *ctx, mpz_t *z, mpz_t *x, mrb_int e)
{
if (e==0)
if (e==0) {
mpz_set(ctx, z, x);
trim(z);
}
else {
short sn = x->sn;
size_t digs = e / DIG_SIZE;
@@ -2835,6 +2837,7 @@ mpz_mul_2exp(mpz_ctx_t *ctx, mpz_t *z, mpz_t *x, mrb_int e)
}
else {
mpz_move(ctx, z, &y);
trim(z);
}
if (uzero_p(z))
z->sn = 0;
@@ -2852,6 +2855,7 @@ mpz_div_2exp(mpz_ctx_t *ctx, mpz_t *z, mpz_t *x, mrb_int e)
mpz_clear(ctx, z);
mpz_init_heap(ctx, z, x->sz);
mpz_set(ctx, z, x);
trim(z);
}
/* else: z == x, nothing to do */
}
@@ -2879,6 +2883,7 @@ mpz_div_2exp(mpz_ctx_t *ctx, mpz_t *z, mpz_t *x, mrb_int e)
}
else {
mpz_move(ctx, z, &y);
trim(z);
}
if (uzero_p(z))
z->sn = 0;