From 6fb93ce6f763e09cad75edbc0be097233a2b9136 Mon Sep 17 00:00:00 2001 From: Hoshiumi Arata Date: Fri, 13 Dec 2024 12:07:34 +0900 Subject: [PATCH] mruby-bigint: fix bug with division of a small number by a bigint; fix #6456 --- mrbgems/mruby-bigint/core/bigint.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/mrbgems/mruby-bigint/core/bigint.c b/mrbgems/mruby-bigint/core/bigint.c index 59c7bbe8d..297ffa7a9 100644 --- a/mrbgems/mruby-bigint/core/bigint.c +++ b/mrbgems/mruby-bigint/core/bigint.c @@ -589,8 +589,12 @@ mpz_mdiv(mrb_state *mrb, mpz_t *q, mpz_t *x, mpz_t *y) if (uzero_p(q)) q->sn = 0; /* now if r != 0 and q < 0 we need to round q towards -inf */ - if (!uzero_p(&r) && qsign < 0) + if (!uzero_p(&r) && qsign < 0) { + /* add 1 to magnitude */ mpz_add_int(mrb, q, 1); + /* force negative sign in case the value of q was zero before rounding */ + q->sn = -1; + } mpz_clear(mrb, &r); } @@ -654,8 +658,12 @@ mpz_mdivmod(mrb_state *mrb, mpz_t *q, mpz_t *r, mpz_t *x, mpz_t *y) if (uzero_p(q)) q->sn = 0; /* now if r != 0 and q < 0 we need to round q towards -inf */ - if (!uzero_p(r) && qsign < 0) + if (!uzero_p(r) && qsign < 0) { + /* add 1 to magnitude */ mpz_add_int(mrb, q, 1); + /* force negative sign in case the value of q was zero before rounding */ + q->sn = -1; + } } static void