summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorvlefevre <vlefevre@280ebfd0-de03-0410-8827-d642c229c3f4>2014-06-26 01:02:26 +0000
committervlefevre <vlefevre@280ebfd0-de03-0410-8827-d642c229c3f4>2014-06-26 01:02:26 +0000
commitb19a82e0c918e8786702ceaad07760bd9436479b (patch)
treeaeef501826ba7ae134eaf69b6fa80c467b381a10
parentb64863635c59b9dec38120e1467ee093e3999baa (diff)
downloadmpfr-b19a82e0c918e8786702ceaad07760bd9436479b.tar.gz
[src/div.c] Bug fix: avoid integer overflow in the code added in r9086
(now tdiv no longer fails). git-svn-id: svn://scm.gforge.inria.fr/svn/mpfr/trunk@9107 280ebfd0-de03-0410-8827-d642c229c3f4
-rw-r--r--src/div.c8
1 files changed, 7 insertions, 1 deletions
diff --git a/src/div.c b/src/div.c
index 5a26a7cf8..bc1d4c8ec 100644
--- a/src/div.c
+++ b/src/div.c
@@ -366,7 +366,13 @@ mpfr_div (mpfr_ptr q, mpfr_srcptr u, mpfr_srcptr v, mpfr_rnd_t rnd_mode)
MPFR_CHANGE_SIGN(q);
}
/* q did not under/overflow */
- MPFR_EXP(q) -= exp_v - GMP_NUMB_BITS;
+ MPFR_EXP(q) -= exp_v;
+ /* The following test is needed, otherwise the next addition
+ on the exponent may overflow, e.g. when dividing the
+ largest finite MPFR number by the smallest positive one. */
+ if (MPFR_UNLIKELY (MPFR_EXP(q) > __gmpfr_emax - GMP_NUMB_BITS))
+ return mpfr_overflow (q, rnd_mode, MPFR_SIGN(q));
+ MPFR_EXP(q) += GMP_NUMB_BITS;
return mpfr_check_range (q, inex, rnd_mode);
}