Skip to content

bpo-31293: Fix crashes in truediv and mul of a timedelta by a float with a bad as_integer_ratio() method #3227

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 19, 2017
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 @@ -866,6 +866,26 @@ def test_divmod(self):

self.assertRaises(TypeError, divmod, t, 10)

def test_issue31293(self):
# The interpreter shouldn't crash in case a timedelta is divided or
# multiplied by a float with a bad as_integer_ratio() method.
def get_bad_float(bad_ratio):
class BadFloat(float):
def as_integer_ratio(self):
return bad_ratio
return BadFloat()

with self.assertRaises(TypeError):
timedelta() / get_bad_float(1 << 1000)
with self.assertRaises(TypeError):
timedelta() * get_bad_float(1 << 1000)

for bad_ratio in [(), (42, ), (1, 2, 3)]:
Copy link
Member

Choose a reason for hiding this comment

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

What would happen if as_integer_ratio() returns a pair of non-integers? A pair of strings? A pair of floats? Could you add tests for these scenarios?

Copy link
Member

Choose a reason for hiding this comment

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

TypeError will be raised on the attempt to divide on a string.

It doesn't matter what errors are raised with a bad as_integer_ratio(), but the code shouldn't crash.

with self.assertRaises(ValueError):
timedelta() / get_bad_float(bad_ratio)
with self.assertRaises(ValueError):
timedelta() * get_bad_float(bad_ratio)


#############################################################################
# date tests
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix crashes in true division and multiplication of a timedelta object by a
float with a bad as_integer_ratio() method. Patch by Oren Milman.
37 changes: 33 additions & 4 deletions Modules/_datetimemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1651,6 +1651,33 @@ multiply_int_timedelta(PyObject *intobj, PyDateTime_Delta *delta)
return result;
}

static PyObject *
get_float_as_integer_ratio(PyObject *floatobj)
{
PyObject *ratio;

assert(floatobj && PyFloat_Check(floatobj));
ratio = _PyObject_CallMethodId(floatobj, &PyId_as_integer_ratio, NULL);
if (ratio == NULL) {
return NULL;
}
if (!PyTuple_Check(ratio)) {
PyErr_Format(PyExc_TypeError,
"unexpected return type from as_integer_ratio(): "
"expected tuple, got '%.200s'",
Py_TYPE(ratio)->tp_name);
Py_DECREF(ratio);
return NULL;
}
if (PyTuple_Size(ratio) != 2) {
PyErr_SetString(PyExc_ValueError,
"as_integer_ratio() must return a 2-tuple");
Py_DECREF(ratio);
return NULL;
}
return ratio;
}

static PyObject *
multiply_float_timedelta(PyObject *floatobj, PyDateTime_Delta *delta)
{
Expand All @@ -1661,9 +1688,10 @@ multiply_float_timedelta(PyObject *floatobj, PyDateTime_Delta *delta)
pyus_in = delta_to_microseconds(delta);
if (pyus_in == NULL)
return NULL;
ratio = _PyObject_CallMethodId(floatobj, &PyId_as_integer_ratio, NULL);
if (ratio == NULL)
ratio = get_float_as_integer_ratio(floatobj);
if (ratio == NULL) {
goto error;
}
temp = PyNumber_Multiply(pyus_in, PyTuple_GET_ITEM(ratio, 0));
Py_DECREF(pyus_in);
pyus_in = NULL;
Expand Down Expand Up @@ -1759,9 +1787,10 @@ truedivide_timedelta_float(PyDateTime_Delta *delta, PyObject *f)
pyus_in = delta_to_microseconds(delta);
if (pyus_in == NULL)
return NULL;
ratio = _PyObject_CallMethodId(f, &PyId_as_integer_ratio, NULL);
if (ratio == NULL)
ratio = get_float_as_integer_ratio(f);
if (ratio == NULL) {
goto error;
}
temp = PyNumber_Multiply(pyus_in, PyTuple_GET_ITEM(ratio, 1));
Py_DECREF(pyus_in);
pyus_in = NULL;
Expand Down