bpo-31752: Fix possible crash in timedelta constructor called with custom integers. (GH-3947) (#4086)

Bad remainder in divmod() in intermediate calculations caused an assertion failure.
(cherry picked from commit 4ffd4653a7)
This commit is contained in:
Miss Islington (bot) 2017-10-23 08:20:13 -07:00 committed by Serhiy Storchaka
parent aaf6a3dbbd
commit 6e45d7b90a
3 changed files with 28 additions and 2 deletions

View file

@ -866,6 +866,26 @@ class TestTimeDelta(HarmlessMixedComparison, unittest.TestCase):
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
timedelta() * get_bad_float(bad_ratio) timedelta() * get_bad_float(bad_ratio)
def test_issue31752(self):
# The interpreter shouldn't crash because divmod() returns negative
# remainder.
class BadInt(int):
def __mul__(self, other):
return Prod()
class Prod:
def __radd__(self, other):
return Sum()
class Sum(int):
def __divmod__(self, other):
# negative remainder
return (0, -1)
timedelta(microseconds=BadInt(1))
timedelta(hours=BadInt(1))
timedelta(weeks=BadInt(1))
############################################################################# #############################################################################
# date tests # date tests

View file

@ -0,0 +1 @@
Fix possible crash in timedelta constructor called with custom integers.

View file

@ -1533,6 +1533,7 @@ delta_to_microseconds(PyDateTime_Delta *self)
if (x2 == NULL) if (x2 == NULL)
goto Done; goto Done;
result = PyNumber_Add(x1, x2); result = PyNumber_Add(x1, x2);
assert(result == NULL || PyLong_CheckExact(result));
Done: Done:
Py_XDECREF(x1); Py_XDECREF(x1);
@ -1555,6 +1556,7 @@ microseconds_to_delta_ex(PyObject *pyus, PyTypeObject *type)
PyObject *num = NULL; PyObject *num = NULL;
PyObject *result = NULL; PyObject *result = NULL;
assert(PyLong_CheckExact(pyus));
tuple = PyNumber_Divmod(pyus, us_per_second); tuple = PyNumber_Divmod(pyus, us_per_second);
if (tuple == NULL) if (tuple == NULL)
goto Done; goto Done;
@ -2108,11 +2110,13 @@ accum(const char* tag, PyObject *sofar, PyObject *num, PyObject *factor,
assert(num != NULL); assert(num != NULL);
if (PyLong_Check(num)) { if (PyLong_Check(num)) {
prod = PyNumber_Multiply(num, factor); prod = PyNumber_Multiply(factor, num);
if (prod == NULL) if (prod == NULL)
return NULL; return NULL;
assert(PyLong_CheckExact(prod));
sum = PyNumber_Add(sofar, prod); sum = PyNumber_Add(sofar, prod);
Py_DECREF(prod); Py_DECREF(prod);
assert(sum == NULL || PyLong_CheckExact(sum));
return sum; return sum;
} }
@ -2155,7 +2159,7 @@ accum(const char* tag, PyObject *sofar, PyObject *num, PyObject *factor,
* fractional part requires float arithmetic, and may * fractional part requires float arithmetic, and may
* lose a little info. * lose a little info.
*/ */
assert(PyLong_Check(factor)); assert(PyLong_CheckExact(factor));
dnum = PyLong_AsDouble(factor); dnum = PyLong_AsDouble(factor);
dnum *= fracpart; dnum *= fracpart;
@ -2170,6 +2174,7 @@ accum(const char* tag, PyObject *sofar, PyObject *num, PyObject *factor,
Py_DECREF(sum); Py_DECREF(sum);
Py_DECREF(x); Py_DECREF(x);
*leftover += fracpart; *leftover += fracpart;
assert(y == NULL || PyLong_CheckExact(y));
return y; return y;
} }