From cb4298e9118be323de3d2f5bbc7e33f5f43fbd2c Mon Sep 17 00:00:00 2001 From: jasonmoxham Date: Fri, 13 Mar 2009 00:42:52 +0000 Subject: [PATCH] fix for mpz_urandomm reuse error --- mpz/urandomm.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/mpz/urandomm.c b/mpz/urandomm.c index 8cc1f393..9c4e9b5a 100644 --- a/mpz/urandomm.c +++ b/mpz/urandomm.c @@ -36,6 +36,7 @@ mpz_urandomm (mpz_ptr rop, gmp_randstate_t rstate, mpz_srcptr n) int count; int pow2; int cmp; + int overlap=0; size = ABSIZ (n); if (size == 0) @@ -52,7 +53,6 @@ mpz_urandomm (mpz_ptr rop, gmp_randstate_t rstate, mpz_srcptr n) pow2 = 0; /* Mark n as `not a power of two'. */ break; } - count_leading_zeros (count, *nlast); nbits = size * GMP_NUMB_BITS - (count - GMP_NAIL_BITS) - pow2; if (nbits == 0) /* nbits == 0 means that n was == 1. */ @@ -61,6 +61,13 @@ mpz_urandomm (mpz_ptr rop, gmp_randstate_t rstate, mpz_srcptr n) return; } + np=PTR(n); + rp=PTR(rop); + if(np==rp) + {overlap=1; + np=__GMP_ALLOCATE_FUNC_LIMBS(size); + MPN_COPY(np,PTR(n),size); + } /* Here the allocated size can be one too much if n is a power of (2^GMP_NUMB_BITS) but it's convenient for using mpn_cmp below. */ rp = MPZ_REALLOC (rop, size); @@ -71,14 +78,15 @@ mpz_urandomm (mpz_ptr rop, gmp_randstate_t rstate, mpz_srcptr n) do { _gmp_rand (rp, rstate, nbits); - MPN_CMP (cmp, rp, PTR (n), size); + MPN_CMP (cmp, rp, np, size); } while (cmp >= 0 && --count != 0); if (count == 0) /* Too many iterations; return result mod n == result - n */ - mpn_sub_n (rp, rp, PTR (n), size); + mpn_sub_n (rp, rp, np, size); + if(overlap)__GMP_FREE_FUNC_LIMBS(np,size); MPN_NORMALIZE (rp, size); SIZ (rop) = size; }