mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
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:
@@ -2812,8 +2812,10 @@ mpz_get_int(mpz_t *y, mrb_int *v)
|
|||||||
static void
|
static void
|
||||||
mpz_mul_2exp(mpz_ctx_t *ctx, mpz_t *z, mpz_t *x, mrb_int e)
|
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);
|
mpz_set(ctx, z, x);
|
||||||
|
trim(z);
|
||||||
|
}
|
||||||
else {
|
else {
|
||||||
short sn = x->sn;
|
short sn = x->sn;
|
||||||
size_t digs = e / DIG_SIZE;
|
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 {
|
else {
|
||||||
mpz_move(ctx, z, &y);
|
mpz_move(ctx, z, &y);
|
||||||
|
trim(z);
|
||||||
}
|
}
|
||||||
if (uzero_p(z))
|
if (uzero_p(z))
|
||||||
z->sn = 0;
|
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_clear(ctx, z);
|
||||||
mpz_init_heap(ctx, z, x->sz);
|
mpz_init_heap(ctx, z, x->sz);
|
||||||
mpz_set(ctx, z, x);
|
mpz_set(ctx, z, x);
|
||||||
|
trim(z);
|
||||||
}
|
}
|
||||||
/* else: z == x, nothing to do */
|
/* 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 {
|
else {
|
||||||
mpz_move(ctx, z, &y);
|
mpz_move(ctx, z, &y);
|
||||||
|
trim(z);
|
||||||
}
|
}
|
||||||
if (uzero_p(z))
|
if (uzero_p(z))
|
||||||
z->sn = 0;
|
z->sn = 0;
|
||||||
|
|||||||
Reference in New Issue
Block a user