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

Conversation

orenmn
Copy link
Contributor

@orenmn orenmn commented Aug 28, 2017

  • in _datetimemodule.c - add checks whether as_integer_ratio() returned a tuple.
  • in datetimetester.py - add tests to verify that the crashes are no more.

https://bugs.python.org/issue31293

class BadFloat(float):
def as_integer_ratio(self):
return 1 << 1000
self.assertRaises(TypeError, truediv, timedelta(), BadFloat())
Copy link
Member

Choose a reason for hiding this comment

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

The modern style to test for exceptions is to use a with assertRaises(..) block:

with self.assertRaises(TypeError):
    timedelta() / BadFloat()

@@ -1664,6 +1664,12 @@ multiply_float_timedelta(PyObject *floatobj, PyDateTime_Delta *delta)
ratio = _PyObject_CallMethodId(floatobj, &PyId_as_integer_ratio, NULL);
if (ratio == NULL)
goto error;
if (!PyTuple_Check(ratio)) {
PyErr_SetString(PyExc_TypeError,
"Can't multiply timedelta object by float with "
Copy link
Member

Choose a reason for hiding this comment

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

It looks like other error messages in this file start with a lowercase letter. Let's keep the style consistent.

I also wonder if a better message would be "unexpected return type from as_integer_ratio(): expected tuple, got %s".

@bedevere-bot
Copy link

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I didn't expect the Spanish Inquisition!. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@orenmn
Copy link
Contributor Author

orenmn commented Aug 28, 2017

I didn't expect the Spanish Inquisition!

@bedevere-bot
Copy link

Nobody expects the Spanish Inquisition!

@abalkin: please review the changes made to this pull request.

@@ -1664,6 +1664,13 @@ multiply_float_timedelta(PyObject *floatobj, PyDateTime_Delta *delta)
ratio = _PyObject_CallMethodId(floatobj, &PyId_as_integer_ratio, NULL);
if (ratio == NULL)
goto error;
if (!PyTuple_Check(ratio)) {
Copy link
Member

Choose a reason for hiding this comment

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

This is not enough. The size of the tuple should be 2.

Perhaps the code can be shared in multiply_float_timedelta() and divide_timedelta_int().

Copy link
Contributor Author

Choose a reason for hiding this comment

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

agh, of course.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

how can code be shared between these two functions? (I wrote a helper-function to share my patch's code between multiply_float_timedelta() and truedivide_timedelta_float().)

Copy link
Member

Choose a reason for hiding this comment

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

The code of multiply_float_timedelta() and divide_timedelta_int() is almost the same. It is enough a one boolean parameter to distinguish multiplication from division. All code can be moved in a separate function, and multiply_float_timedelta() and divide_timedelta_int() will call it with additional argument 0 or 1.

Usually a refactoring is made only in develop version, but I think this one can be done in a bugfix change. It is enough simple and can help to fix other bugs if they will be found. What are your thoughts @abalkin?

@bedevere-bot
Copy link

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I didn't expect the Spanish Inquisition!. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

And if you don't make the requested changes, you will be put in the comfy chair!

@orenmn
Copy link
Contributor Author

orenmn commented Aug 28, 2017

I am working to fix the problems that Serhiy pointed out. please don't merge.

@orenmn
Copy link
Contributor Author

orenmn commented Aug 28, 2017

I didn't expect the Spanish Inquisition!

@bedevere-bot
Copy link

Nobody expects the Spanish Inquisition!

@serhiy-storchaka, @abalkin: please review the changes made to this pull request.

Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

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

LGTM. I have added just a nitpick.

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):
Copy link
Member

Choose a reason for hiding this comment

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

No starting underscore is needed.

@serhiy-storchaka serhiy-storchaka added needs backport to 3.6 type-bug An unexpected behavior, bug, or error labels Sep 19, 2017
@serhiy-storchaka serhiy-storchaka merged commit 865e4b4 into python:master Sep 19, 2017
@miss-islington
Copy link
Contributor

Thanks @orenmn for the PR, and @serhiy-storchaka for merging it 🌮🎉.. I'm working now to backport this PR to: 3.6.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Sep 19, 2017
…loat with a bad as_integer_ratio() method. (pythonGH-3227)

(cherry picked from commit 865e4b4)
@bedevere-bot
Copy link

GH-3654 is a backport of this pull request to the 3.6 branch.

serhiy-storchaka pushed a commit that referenced this pull request Sep 19, 2017
…loat with a bad as_integer_ratio() method. (GH-3227) (#3654)

(cherry picked from commit 865e4b4)
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.

@miss-islington
Copy link
Contributor

Thanks @orenmn for the PR, and @serhiy-storchaka for merging it 🌮🎉.. I'm working now to backport this PR to: 3.6.
🐍🍒⛏🤖

@miss-islington
Copy link
Contributor

Sorry, @orenmn and @serhiy-storchaka, I could not cleanly backport this to 3.6 due to a conflict.
Please backport using cherry_picker on command line.
cherry_picker 865e4b4f630e2ae91e61239258abb58b488f1d65 3.6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants