Skip to content

Conversation

@orenmn
Copy link
Contributor

@orenmn orenmn commented Sep 27, 2017

  • in _collectionsmodule.c: add a check whether __new__() returned a deque.

https://bugs.python.org/issue31608

deque, old_deque->maxlen, NULL);
}
if (result != NULL &&
!PyObject_IsInstance(result, (PyObject *)&deque_type))
Copy link
Member

Choose a reason for hiding this comment

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

  1. PyObject_IsInstance() can raise an exception.
  2. Even if PyObject_IsInstance() returns 1, the result can have the binary layout different from dequeobject (because __instancecheck__), and the following concatenation or repeating will crash. Use PyObject_TypeCheck() instead.

!PyObject_IsInstance(result, (PyObject *)&deque_type))
{
PyErr_Format(PyExc_TypeError,
"%.200s.__new__() must return a deque, not %.200s",
Copy link
Member

Choose a reason for hiding this comment

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

Bad __new__() is not the only possible cause. The type can be an instance of a metaclass with overridden __call__.

@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.

@serhiy-storchaka
Copy link
Member

@rhettinger, please take a look at this patch.

Copy link
Contributor

@rhettinger rhettinger left a comment

Choose a reason for hiding this comment

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

Please add a test.

else {
result = PyObject_CallFunction((PyObject *)(Py_TYPE(deque)), "Oi",
deque, old_deque->maxlen, NULL);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Please restore 541-548 to their former state and limit the patch to just the issue at hand.

Py_DECREF(result);
return NULL;
}
return result;
Copy link
Contributor

Choose a reason for hiding this comment

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

This looks fine but it needs a test.

@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 have made the requested changes; please review again. 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.

pass
d = X()
def bad___new__(cls, *args, **kwargs):
return 42
Copy link
Member

Choose a reason for hiding this comment

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

I think returning a list is more realistic example.

return 42
X.__new__ = bad___new__
with self.assertRaises(TypeError):
d * 42 # shouldn't crash
Copy link
Member

Choose a reason for hiding this comment

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

What about concatenation?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The C changes are only to deque_copy(), so i thought that testing also the concatenation is redundant.
Should i add it to the test anyway?

@@ -0,0 +1,2 @@
Raise a ``TypeError`` instead of crashing, in specific cases where a
``collections.deque`` subclass returns a non-deque. Patch by Oren Milman.
Copy link
Member

Choose a reason for hiding this comment

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

I would mention a copy.

Copy link
Contributor Author

@orenmn orenmn Oct 29, 2017

Choose a reason for hiding this comment

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

I think that mentioning the copy won't help much to a user that isn't familiar with the internal implementation.
If you think that i should be more specific, i could mention that the crashes were in repetition and concatenation.
What do you think?

@orenmn
Copy link
Contributor Author

orenmn commented Oct 29, 2017

I have made the requested changes; please review again.

@bedevere-bot
Copy link

Thanks for making the requested changes!

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

@benjaminp benjaminp merged commit 24bd50b into python:master Sep 11, 2018
@miss-islington
Copy link
Contributor

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

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Sep 11, 2018
…s.deque with a bad __new__(). (pythonGH-3788)

(cherry picked from commit 24bd50b)

Co-authored-by: Oren Milman <[email protected]>
@bedevere-bot
Copy link

GH-9177 is a backport of this pull request to the 3.7 branch.

@miss-islington
Copy link
Contributor

Sorry, @orenmn and @benjaminp, I could not cleanly backport this to 3.6 due to a conflict.
Please backport using cherry_picker on command line.
cherry_picker 24bd50bdcc97d65130c07d6cd26085fd06c3e972 3.6

@miss-islington
Copy link
Contributor

Sorry, @orenmn and @benjaminp, I could not cleanly backport this to 2.7 due to a conflict.
Please backport using cherry_picker on command line.
cherry_picker 24bd50bdcc97d65130c07d6cd26085fd06c3e972 2.7

benjaminp pushed a commit that referenced this pull request Sep 11, 2018
…ections.deque with a bad __new__(). (GH-3788).

(cherry picked from commit 24bd50b)

Co-authored-by: Oren Milman <[email protected]>
benjaminp pushed a commit that referenced this pull request Sep 11, 2018
…ections.deque with a bad __new__(). (GH-3788).

(cherry picked from commit 24bd50b)

Co-authored-by: Oren Milman <[email protected]>
miss-islington added a commit that referenced this pull request Sep 11, 2018
…s.deque with a bad __new__(). (GH-3788)

(cherry picked from commit 24bd50b)

Co-authored-by: Oren Milman <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants