mini-gmp: mpz_init_set_str fails on leading zeroes

Austyn Krutsinger akrutsinger at gmail.com
Thu Jul 21 08:53:16 UTC 2016


On Thu, Jul 21, 2016 at 1:43 AM, Torbjörn Granlund <tg at gmplib.org> wrote:

> Austyn Krutsinger <akrutsinger at gmail.com> writes:
>
>   My initial though was to just skip the leading zero's in the mpz_set_str
>   function by something like this:
>
>     while (isspace( (unsigned char) *sp) || (*sp == '0'))
>       sp++;
>
>   Only problems is that this doesn't work for negative numbers that still
>   have a bunch of leading zeros;
>
> I think we should not accept strings like 00000-0000017.
>

Absolutely agree with you, kind of a silly proposal in retrospect.

We can fix this in the mpn_set_str_other function by changing the
comparison in line 1321. If we accept that w can be > or = to 0, then there
is no issue if the number has leading zeros. So line 1321 in
mpn_set_str_other becomes:

1321   for (rn = (w >= 0); j < sn;)
1322     {
1323       mp_limb_t cy;
1324
1325       w = sp[j++];
1326       for (k = 1; k < info->exp; k++)
1327     w = w * b + sp[j++];
1328
1329       cy = mpn_mul_1 (rp, rp, rn, info->bb);
1330       cy += mpn_add_1 (rp, rp, rn, w);
1331       if (cy > 0)
1332     rp[rn++] = cy;
1333     }
1334   assert (j == sn);
1321   for (rn = (w > 0); j < sn;)

Regards,
Austyn


More information about the gmp-bugs mailing list