mini-gmp: mpz_init_set_str fails on leading zeroes

Niels Möller nisse at lysator.liu.se
Thu Jul 28 08:00:56 UTC 2016


Austyn Krutsinger <akrutsinger at gmail.com> writes:

> 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;)

Checking what the fine manual says on mpn_set_str:

     The converted value is {RP,RN} where RN is the return value.  If
     the most significant input byte STR[0] is non-zero, then RP[RN-1]
     will be non-zero, else RP[RN-1] and some number of subsequent limbs
     may be zero.

So I think it's fine to always initialize rn to 1. And then mpz_set_str
will have to either strip leading zeros in the input to mpn_set_str, or
normalize the result afterwards.

Since this is mini-gmp, another option which is fine if it turns
out to be simpler, is to add an if (!n) return 0; to the start of
mpn_mul_1, and an appropriate comment somewhere. Then mpn_set_str will
produce always normalized output, with no changes.

Regards,
/Niels

-- 
Niels Möller. PGP-encrypted email is preferred. Keyid C0B98E26.
Internet email is subject to wholesale government surveillance.


More information about the gmp-bugs mailing list