bigint.c (mpz_get_str): fixed wrong loop termination condition.

This commit is contained in:
Yukihiro "Matz" Matsumoto
2022-07-21 19:26:12 +09:00
parent 450382735b
commit 8bd1c56465
+6 -5
View File
@@ -688,15 +688,16 @@ mpz_get_str(mrb_state *mrb, char *s, mrb_int sz, mrb_int base, mpz_t *x)
char *se = s+sz;
int xlen = digits(x);
mp_limb *t = (mp_limb*)mrb_malloc(mrb, xlen*sizeof(mp_limb));
mp_limb *tend = t + xlen;
memcpy(t, x->p, xlen*sizeof(mp_limb));
mp_limb b2 = base;
const int blim = (sizeof(mp_limb)==sizeof(int))?(base<=10?4:3):(base<=10?9:5);
const int blim = (sizeof(mp_limb)==4)?(base<=10?4:3):(base<=10?9:5);
for (int i=1; i<blim; i++) {
b2 *= base;
}
for (;;) {
mp_limb *d = t + xlen;
mp_limb *d = tend;
mp_limb a = 0;
while (--d >= t) {
mp_limb d0 = *d, d1;
@@ -720,14 +721,14 @@ mpz_get_str(mrb_state *mrb, char *s, mrb_int sz, mrb_int base, mpz_t *x)
}
// check if number is zero
for (d = t; d < t + xlen; ++d) {
for (d = t; d < tend; d++) {
if (*d != 0) break;
while (ps<s && s[-1]=='0') s--;
goto done;
}
if (d == tend) goto done;
}
done:
while (ps<s && s[-1]=='0') s--;
mrb_free(mrb, t);
if (x->sn < 0) {
*s++ = '-';