Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions Lib/test/datetimetester.py
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,26 @@ def as_integer_ratio(self):
with self.assertRaises(ValueError):
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:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe inherit from int as well?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it shouldn't be inherited from int, otherwise __radd__ would not be called.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh ok.

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
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 || 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(PyLong_CheckExact(pyus));
tuple = PyNumber_Divmod(pyus, us_per_second);
if (tuple == NULL)
goto Done;
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
Expand All @@ -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;
}

Expand Down