Skip to content

bpo-39421: Fix posible crash in heapq with custom comparison operators #18118

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 3 commits into from
Jan 23, 2020

Conversation

pablogsal
Copy link
Member

@pablogsal pablogsal commented Jan 22, 2020

Copy link
Member

@corona10 corona10 left a comment

Choose a reason for hiding this comment

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

import heapq

class h(int):
    def __lt__(self, o):
        list2.clear()
        return NotImplemented

class g(int):
    def __lt__(self, o):
        list1.clear()
        return NotImplemented

list1, list2 = [], []

heapq.heappush(list1, h(0))
heapq.heappush(list2, g(0))

print(list1)
print(list2)

heapq.heappush(list1, g(1))
heapq.heappush(list2, h(1))
print(list1)
print(list2)

This PR does not fix this case. Please take a look

@pablogsal
Copy link
Member Author

This PR does not fix this case. Please take a look

Thanks! I added a test in 9718f66

Copy link
Member

@corona10 corona10 left a comment

Choose a reason for hiding this comment

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

LGTM

Just comment:
In some time, if we can pass objects as a variadic argument.
The code might be cleaner. (I don't deeply think about the possibility)

e.g. Py_INCREF(a, b, c...); Py_DECREF(a, b, c...);

@pablogsal
Copy link
Member Author

pablogsal commented Jan 22, 2020

e.g. Py_INCREF(a, b, c...); Py_DECREF(a, b, c...);

We would need a different function as C does not allow overloads (for va_list in this case) and I don't think is that common

@pablogsal
Copy link
Member Author

I would like to wait to see if @vstinner and @methane would prefer to add the check into PyObject_RichCompareBool instead although I think it would be too costly.

@corona10 corona10 requested review from vstinner and methane January 22, 2020 16:37
@alex
Copy link
Member

alex commented Jan 23, 2020

This should be backported to all branches that are still doing security releases.

@pablogsal pablogsal merged commit 79f89e6 into python:master Jan 23, 2020
@pablogsal pablogsal deleted the bpo-39421 branch January 23, 2020 14:07
@miss-islington
Copy link
Contributor

Thanks @pablogsal for the PR 🌮🎉.. I'm working now to backport this PR to: 3.6, 3.7, 3.8.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jan 23, 2020
pythonGH-18118)

* bpo-39421: Fix posible crash in heapq with custom comparison operators

* fixup! bpo-39421: Fix posible crash in heapq with custom comparison operators

* fixup! fixup! bpo-39421: Fix posible crash in heapq with custom comparison operators
(cherry picked from commit 79f89e6)

Co-authored-by: Pablo Galindo <[email protected]>
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jan 23, 2020
pythonGH-18118)

* bpo-39421: Fix posible crash in heapq with custom comparison operators

* fixup! bpo-39421: Fix posible crash in heapq with custom comparison operators

* fixup! fixup! bpo-39421: Fix posible crash in heapq with custom comparison operators
(cherry picked from commit 79f89e6)

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

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

@bedevere-bot
Copy link

GH-18144 is a backport of this pull request to the 3.8 branch.

@bedevere-bot
Copy link

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

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jan 23, 2020
pythonGH-18118)

* bpo-39421: Fix posible crash in heapq with custom comparison operators

* fixup! bpo-39421: Fix posible crash in heapq with custom comparison operators

* fixup! fixup! bpo-39421: Fix posible crash in heapq with custom comparison operators
(cherry picked from commit 79f89e6)

Co-authored-by: Pablo Galindo <[email protected]>
miss-islington added a commit that referenced this pull request Jan 23, 2020
GH-18118)

* bpo-39421: Fix posible crash in heapq with custom comparison operators

* fixup! bpo-39421: Fix posible crash in heapq with custom comparison operators

* fixup! fixup! bpo-39421: Fix posible crash in heapq with custom comparison operators
(cherry picked from commit 79f89e6)

Co-authored-by: Pablo Galindo <[email protected]>
ned-deily pushed a commit that referenced this pull request Jan 23, 2020
@miss-islington
Copy link
Contributor

Thanks @pablogsal for the PR 🌮🎉.. I'm working now to backport this PR to: 3.8.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jan 23, 2020
pythonGH-18118)

* bpo-39421: Fix posible crash in heapq with custom comparison operators

* fixup! bpo-39421: Fix posible crash in heapq with custom comparison operators

* fixup! fixup! bpo-39421: Fix posible crash in heapq with custom comparison operators
(cherry picked from commit 79f89e6)

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

GH-18149 is a backport of this pull request to the 3.8 branch.

miss-islington added a commit that referenced this pull request Jan 23, 2020
GH-18118)

* bpo-39421: Fix posible crash in heapq with custom comparison operators

* fixup! bpo-39421: Fix posible crash in heapq with custom comparison operators

* fixup! fixup! bpo-39421: Fix posible crash in heapq with custom comparison operators
(cherry picked from commit 79f89e6)

Co-authored-by: Pablo Galindo <[email protected]>
shihai1991 pushed a commit to shihai1991/cpython that referenced this pull request Jan 31, 2020
pythonGH-18118)

* bpo-39421: Fix posible crash in heapq with custom comparison operators

* fixup! bpo-39421: Fix posible crash in heapq with custom comparison operators

* fixup! fixup! bpo-39421: Fix posible crash in heapq with custom comparison operators
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