Skip to content

Commit

Permalink
[2.7] bpo-31752: Fix possible crash in timedelta constructor called w…
Browse files Browse the repository at this point in the history
…ith custom integers. (GH-3947) (#4088)

Bad remainder in divmod() in intermediate calculations caused an assertion failure..
(cherry picked from commit 4ffd465)
  • Loading branch information
serhiy-storchaka committed Oct 23, 2017
1 parent f7d19b0 commit 5ef883b
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
40 changes: 40 additions & 0 deletions Lib/test/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,46 @@ def as_hours(self):
self.assertEqual(str(t3), str(t4))
self.assertEqual(t4.as_hours(), -1)

def test_issue31752(self):
# The interpreter shouldn't crash because divmod() returns negative
# remainder.
class BadInt(int):
def __mul__(self, other):
return Prod()

class BadLong(long):
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))
timedelta(microseconds=BadLong(1))
timedelta(hours=BadLong(1))
timedelta(weeks=BadLong(1))

class Sum(long):
def __divmod__(self, other):
# negative remainder
return (0, -1)

timedelta(microseconds=BadInt(1))
timedelta(hours=BadInt(1))
timedelta(weeks=BadInt(1))
timedelta(microseconds=BadLong(1))
timedelta(hours=BadLong(1))
timedelta(weeks=BadLong(1))


#############################################################################
# date tests

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix possible crash in timedelta constructor called with custom integers.
9 changes: 7 additions & 2 deletions Modules/datetimemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1537,6 +1537,7 @@ delta_to_microseconds(PyDateTime_Delta *self)
if (x2 == NULL)
goto Done;
result = PyNumber_Add(x1, x2);
assert(result == NULL || PyInt_CheckExact(result) || PyLong_CheckExact(result));

Done:
Py_XDECREF(x1);
Expand All @@ -1559,6 +1560,7 @@ microseconds_to_delta_ex(PyObject *pyus, PyTypeObject *type)
PyObject *num = NULL;
PyObject *result = NULL;

assert(PyInt_CheckExact(pyus) || PyLong_CheckExact(pyus));
tuple = PyNumber_Divmod(pyus, us_per_second);
if (tuple == NULL)
goto Done;
Expand Down Expand Up @@ -1851,11 +1853,13 @@ accum(const char* tag, PyObject *sofar, PyObject *num, PyObject *factor,
assert(num != NULL);

if (PyInt_Check(num) || PyLong_Check(num)) {
prod = PyNumber_Multiply(num, factor);
prod = PyNumber_Multiply(factor, num);
if (prod == NULL)
return NULL;
assert(PyInt_CheckExact(prod) || PyLong_CheckExact(prod));
sum = PyNumber_Add(sofar, prod);
Py_DECREF(prod);
assert(sum == NULL || PyInt_CheckExact(sum) || PyLong_CheckExact(sum));
return sum;
}

Expand Down Expand Up @@ -1898,7 +1902,7 @@ accum(const char* tag, PyObject *sofar, PyObject *num, PyObject *factor,
* fractional part requires float arithmetic, and may
* lose a little info.
*/
assert(PyInt_Check(factor) || PyLong_Check(factor));
assert(PyInt_CheckExact(factor) || PyLong_CheckExact(factor));
if (PyInt_Check(factor))
dnum = (double)PyInt_AsLong(factor);
else
Expand All @@ -1916,6 +1920,7 @@ accum(const char* tag, PyObject *sofar, PyObject *num, PyObject *factor,
Py_DECREF(sum);
Py_DECREF(x);
*leftover += fracpart;
assert(y == NULL || PyInt_CheckExact(y) || PyLong_CheckExact(y));
return y;
}

Expand Down

0 comments on commit 5ef883b

Please sign in to comment.