diff options
author | vlefevre <vlefevre@280ebfd0-de03-0410-8827-d642c229c3f4> | 2012-03-08 14:11:02 +0000 |
---|---|---|
committer | vlefevre <vlefevre@280ebfd0-de03-0410-8827-d642c229c3f4> | 2012-03-08 14:11:02 +0000 |
commit | 5ed9d0b72b6abb1f49ae5faec20042cb371a8f30 (patch) | |
tree | d5ea8b8368259678003d4ebea1890680e9975a6c /src/urandomb.c | |
parent | 04037a85b152525d13026c3ff450f01aab1bbd82 (diff) | |
download | mpfr-5ed9d0b72b6abb1f49ae5faec20042cb371a8f30.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.
Added a large precision test in tests/tinits.c (enabled by setting
MPFR_CHECK_LARGEMEM=1) to trigger the bug mentioned above on 32-bit
machines (or when MPFR is built with -m32).
(merged changesets r8025,8026,8029,8031,8077 from the trunk)
git-svn-id: svn://scm.gforge.inria.fr/svn/mpfr/branches/3.1@8080 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); } |