mini-gmp: mpz_abs_sub_bit needs more normalization

Niels Möller nisse at lysator.liu.se
Mon Aug 25 21:21:05 UTC 2014


I've found a bug in mini-gmp. mpz_clrbit fails and produces an
unnormalized mpz, if one clears the most significant one bit of a
number, and that one bit is followed by a long string of zeros.

Fix:

--- a/mini-gmp.c
+++ b/mini-gmp.c
@@ -3569,7 +3569,7 @@ mpz_abs_sub_bit (mpz_t d, mp_bitcnt_t bit_index)
 
   gmp_assert_nocarry (mpn_sub_1 (dp + limb_index, dp + limb_index,
 				 dn - limb_index, bit));
-  dn -= (dp[dn-1] == 0);
+  dn = mpn_normalized_size (dp, dn);
   d->_mp_size = (d->_mp_size < 0) ? - dn : dn;
 }
 
We should also add a test case.

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