summaryrefslogtreecommitdiffstats
path: root/Modules/_datetimemodule.c
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2017-10-23 14:12:28 (GMT)
committerGitHub <noreply@github.com>2017-10-23 14:12:28 (GMT)
commit4ffd4653a7ec9c97775472276cf5e159e2366bb2 (patch)
treedde6087a2aa8c149de6cc7894a5e809ba4f2b32a /Modules/_datetimemodule.c
parent7fed7bd8bb628f0f09c6011871a4ce68afb41b18 (diff)
downloadcpython-4ffd4653a7ec9c97775472276cf5e159e2366bb2.zip
cpython-4ffd4653a7ec9c97775472276cf5e159e2366bb2.tar.gz
cpython-4ffd4653a7ec9c97775472276cf5e159e2366bb2.tar.bz2
bpo-31752: Fix possible crash in timedelta constructor called with custom integers. (#3947)
Bad remainder in divmod() in intermediate calculations caused an assertion failure.
Diffstat (limited to 'Modules/_datetimemodule.c')
-rw-r--r--Modules/_datetimemodule.c9
1 files changed, 7 insertions, 2 deletions
diff --git a/Modules/_datetimemodule.c b/Modules/_datetimemodule.c
index fc2cdba..09b5579 100644
--- a/Modules/_datetimemodule.c
+++ b/Modules/_datetimemodule.c
@@ -1537,6 +1537,7 @@ delta_to_microseconds(PyDateTime_Delta *self)
if (x2 == NULL)
goto Done;
result = PyNumber_Add(x1, x2);
+ assert(result == NULL || PyLong_CheckExact(result));
Done:
Py_XDECREF(x1);
@@ -1559,6 +1560,7 @@ microseconds_to_delta_ex(PyObject *pyus, PyTypeObject *type)
PyObject *num = NULL;
PyObject *result = NULL;
+ assert(PyLong_CheckExact(pyus));
tuple = PyNumber_Divmod(pyus, us_per_second);
if (tuple == NULL)
goto Done;
@@ -2081,11 +2083,13 @@ accum(const char* tag, PyObject *sofar, PyObject *num, PyObject *factor,
assert(num != NULL);
if (PyLong_Check(num)) {
- prod = PyNumber_Multiply(num, factor);
+ prod = PyNumber_Multiply(factor, num);
if (prod == NULL)
return NULL;
+ assert(PyLong_CheckExact(prod));
sum = PyNumber_Add(sofar, prod);
Py_DECREF(prod);
+ assert(sum == NULL || PyLong_CheckExact(sum));
return sum;
}
@@ -2128,7 +2132,7 @@ accum(const char* tag, PyObject *sofar, PyObject *num, PyObject *factor,
* fractional part requires float arithmetic, and may
* lose a little info.
*/
- assert(PyLong_Check(factor));
+ assert(PyLong_CheckExact(factor));
dnum = PyLong_AsDouble(factor);
dnum *= fracpart;
@@ -2143,6 +2147,7 @@ accum(const char* tag, PyObject *sofar, PyObject *num, PyObject *factor,
Py_DECREF(sum);
Py_DECREF(x);
*leftover += fracpart;
+ assert(y == NULL || PyLong_CheckExact(y));
return y;
}