diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2017-09-07 18:35:03 (GMT) |
---|---|---|
committer | Benjamin Peterson <benjamin@python.org> | 2017-09-07 18:35:03 (GMT) |
commit | b03623227ed1264e3cac4e6bb4878d96b91aa484 (patch) | |
tree | 019511e476f77bf6ed4e2cbfd9c8280ff89d9e96 /Python | |
parent | b0d0217c0e4c1512a06ef306928b2fd8f82d046e (diff) | |
download | cpython-b03623227ed1264e3cac4e6bb4878d96b91aa484.zip cpython-b03623227ed1264e3cac4e6bb4878d96b91aa484.tar.gz cpython-b03623227ed1264e3cac4e6bb4878d96b91aa484.tar.bz2 |
[3.6] fixes bpo-31373: fix undefined floating-point demotions (GH-3396) (#3424)
(cherry picked from commit a853a8ba7850381d49b284295dd6f0dc491dbe44)
Diffstat (limited to 'Python')
-rw-r--r-- | Python/getargs.c | 5 | ||||
-rw-r--r-- | Python/pytime.c | 32 |
2 files changed, 21 insertions, 16 deletions
diff --git a/Python/getargs.c b/Python/getargs.c index 616c6eb..8fb19f3 100644 --- a/Python/getargs.c +++ b/Python/getargs.c @@ -4,6 +4,7 @@ #include "Python.h" #include <ctype.h> +#include <float.h> #ifdef __cplusplus @@ -810,6 +811,10 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags, double dval = PyFloat_AsDouble(arg); if (PyErr_Occurred()) RETURN_ERR_OCCURRED; + else if (dval > FLT_MAX) + *p = (float)INFINITY; + else if (dval < -FLT_MAX) + *p = (float)-INFINITY; else *p = (float) dval; break; diff --git a/Python/pytime.c b/Python/pytime.c index 3015a6b..387657a 100644 --- a/Python/pytime.c +++ b/Python/pytime.c @@ -97,7 +97,7 @@ static int _PyTime_DoubleToDenominator(double d, time_t *sec, long *numerator, double denominator, _PyTime_round_t round) { - double intpart, err; + double intpart; /* volatile avoids optimization changing how numbers are rounded */ volatile double floatpart; @@ -115,14 +115,13 @@ _PyTime_DoubleToDenominator(double d, time_t *sec, long *numerator, } assert(0.0 <= floatpart && floatpart < denominator); - *sec = (time_t)intpart; - *numerator = (long)floatpart; - - err = intpart - (double)*sec; - if (err <= -1.0 || err >= 1.0) { + if (!_Py_InIntegralTypeRange(time_t, intpart)) { error_time_t_overflow(); return -1; } + *sec = (time_t)intpart; + *numerator = (long)floatpart; + return 0; } @@ -150,7 +149,7 @@ int _PyTime_ObjectToTime_t(PyObject *obj, time_t *sec, _PyTime_round_t round) { if (PyFloat_Check(obj)) { - double intpart, err; + double intpart; /* volatile avoids optimization changing how numbers are rounded */ volatile double d; @@ -158,12 +157,11 @@ _PyTime_ObjectToTime_t(PyObject *obj, time_t *sec, _PyTime_round_t round) d = _PyTime_Round(d, round); (void)modf(d, &intpart); - *sec = (time_t)intpart; - err = intpart - (double)*sec; - if (err <= -1.0 || err >= 1.0) { + if (!_Py_InIntegralTypeRange(time_t, intpart)) { error_time_t_overflow(); return -1; } + *sec = (time_t)intpart; return 0; } else { @@ -180,7 +178,9 @@ _PyTime_ObjectToTimespec(PyObject *obj, time_t *sec, long *nsec, { int res; res = _PyTime_ObjectToDenominator(obj, sec, nsec, 1e9, round); - assert(0 <= *nsec && *nsec < SEC_TO_NS); + if (res == 0) { + assert(0 <= *nsec && *nsec < SEC_TO_NS); + } return res; } @@ -190,7 +190,9 @@ _PyTime_ObjectToTimeval(PyObject *obj, time_t *sec, long *usec, { int res; res = _PyTime_ObjectToDenominator(obj, sec, usec, 1e6, round); - assert(0 <= *usec && *usec < SEC_TO_US); + if (res == 0) { + assert(0 <= *usec && *usec < SEC_TO_US); + } return res; } @@ -276,7 +278,6 @@ static int _PyTime_FromFloatObject(_PyTime_t *t, double value, _PyTime_round_t round, long unit_to_ns) { - double err; /* volatile avoids optimization changing how numbers are rounded */ volatile double d; @@ -285,12 +286,11 @@ _PyTime_FromFloatObject(_PyTime_t *t, double value, _PyTime_round_t round, d *= (double)unit_to_ns; d = _PyTime_Round(d, round); - *t = (_PyTime_t)d; - err = d - (double)*t; - if (fabs(err) >= 1.0) { + if (!_Py_InIntegralTypeRange(_PyTime_t, d)) { _PyTime_overflow(); return -1; } + *t = (_PyTime_t)d; return 0; } |