summaryrefslogtreecommitdiff
path: root/Python/pytime.c
diff options
context:
space:
mode:
authorBenjamin Peterson <benjamin@python.org>2017-09-07 11:13:59 -0700
committerGitHub <noreply@github.com>2017-09-07 11:13:59 -0700
commita853a8ba7850381d49b284295dd6f0dc491dbe44 (patch)
treedb901475288d8942c221c336fc4ad61f596761c3 /Python/pytime.c
parentc988ae01fec2e0510d53728e01a5e4bb06761bda (diff)
downloadcpython-git-a853a8ba7850381d49b284295dd6f0dc491dbe44.tar.gz
bpo-31373: fix undefined floating-point demotions (#3396)
Diffstat (limited to 'Python/pytime.c')
-rw-r--r--Python/pytime.c32
1 files changed, 16 insertions, 16 deletions
diff --git a/Python/pytime.c b/Python/pytime.c
index 8979adc219..f3c913226c 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;
}