From 61aa2234d8304db79ba1fd63b3e2e72946ed362a Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Mon, 12 Jan 2026 22:27:04 +0900 Subject: [PATCH] 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 --- mrbgems/mruby-bigint/core/bigint.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/mrbgems/mruby-bigint/core/bigint.c b/mrbgems/mruby-bigint/core/bigint.c index 17fd1003d..894f75ac2 100644 --- a/mrbgems/mruby-bigint/core/bigint.c +++ b/mrbgems/mruby-bigint/core/bigint.c @@ -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;