diff options
author | vlefevre <vlefevre@280ebfd0-de03-0410-8827-d642c229c3f4> | 2012-02-22 03:53:19 +0000 |
---|---|---|
committer | vlefevre <vlefevre@280ebfd0-de03-0410-8827-d642c229c3f4> | 2012-02-22 03:53:19 +0000 |
commit | 89b1f422fad8c50cd46b69748624d312922ef881 (patch) | |
tree | dad278625378912197929a89552863c7942f688b /src/urandomb.c | |
parent | 6e9a7ddd09869245d0f46db6493370e0d9a98e54 (diff) | |
download | mpfr-89b1f422fad8c50cd46b69748624d312922ef881.tar.gz |
Avoid potential integer overflows and improve consistency. This should
fix bug #13918 "Segfault with precision = MPFR_PREC_MAX on 32-bit".
Note: this problem appeared in MPFR 3.0.0 when the precision type (now
mpfr_prec_t) was changed to a signed integer.
git-svn-id: svn://scm.gforge.inria.fr/svn/mpfr/trunk@8025 280ebfd0-de03-0410-8827-d642c229c3f4
Diffstat (limited to 'src/urandomb.c')
-rw-r--r-- | src/urandomb.c | 11 |
1 files changed, 9 insertions, 2 deletions
diff --git a/src/urandomb.c b/src/urandomb.c index f8597075d..3e941e7bc 100644 --- a/src/urandomb.c +++ b/src/urandomb.c @@ -31,13 +31,20 @@ http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc., a sufficient number of limbs */ void mpfr_rand_raw (mpfr_limb_ptr mp, gmp_randstate_t rstate, - unsigned long int nbits) + mpfr_prec_t nbits) { mpz_t z; + MPFR_ASSERTN (nbits >= 1); /* To be sure to avoid the potential allocation of mpz_urandomb */ - ALLOC(z) = SIZ(z) = ((nbits - 1) / GMP_NUMB_BITS) + 1; + ALLOC(z) = SIZ(z) = MPFR_PREC2LIMBS (nbits); PTR(z) = mp; +#if __MPFR_GMP(5,0,0) + /* Check for integer overflow (unless mp_bitcnt_t is signed, + but according to the GMP manual, this shouldn't happen). + Note: mp_bitcnt_t has been introduced in GMP 5.0.0. */ + MPFR_ASSERTN ((mp_bitcnt_t) -1 < 0 || nbits <= (mp_bitcnt_t) -1); +#endif mpz_urandomb (z, rstate, nbits); } |