Skip to content

bpo-35961: Fix a crash in slice_richcompare()#11828

Closed
vstinner wants to merge 1 commit into
python:masterfrom
vstinner:slice_richcompare
Closed

bpo-35961: Fix a crash in slice_richcompare()#11828
vstinner wants to merge 1 commit into
python:masterfrom
vstinner:slice_richcompare

Conversation

@vstinner

@vstinner vstinner commented Feb 12, 2019

Copy link
Copy Markdown
Member

Fix a crash in slice_richcompare(): ensure that the 2 temporary
internal tuples used for the comparison are not tracked by the
garbage collector, since they use borrowed references.

The crash (or assertion error) occurred if a garbage collection
occurred during slice_richcompare(), especially while calling
PyObject_RichCompare(t1, t2, op).

https://bugs.python.org/issue35961

Fix a crash in slice_richcompare(): ensure that the 2 temporary
internal tuples used for the comparison are not tracked by the
garbage collector, since they use borrowed references.

The crash (or assertion error) occurred if a garbage collection
occurred during slice_richcompare(), especially while calling
PyObject_RichCompare(t1, t2, op).
@vstinner

Copy link
Copy Markdown
Member Author

I tested manually the fix using https://bugs.python.org/issue35961#msg335333 patch.

Without this change, "./python -m test -F -j0 test_slice test_slice test_slice test_slice test_slice test_slice test_slice test_slice" fails in less than 50 runs.

With this change, I had to interrupt the command after 1500 runs (no crash).

@vstinner vstinner requested a review from pablogsal February 12, 2019 18:30

@pablogsal pablogsal left a comment

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.

LGTM

Only one small comment

Comment thread Objects/sliceobject.c
if (t1 == NULL) {
return NULL;
}
PyObject_GC_UnTrack(t1);

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.

Does it makes sense to add a small comment here explaining why we untrack the tuples?

@pablogsal

Copy link
Copy Markdown
Member

Also, isn't it stolen references instead of borrowed references?

@tim-one

tim-one commented Feb 12, 2019

Copy link
Copy Markdown
Member

Also, isn't it stolen references

Yup, PyTuple_SET_ITEM(op, i, v) steals the slice object's reference to v: the tuple then owns the reference, no longer the slice object.

@tim-one tim-one left a comment

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.

Good catch! I agree with the earlier comments, especially that this is obscure enough to deserve a comment about why the temp tuples are being exempted from gc.

@vstinner

Copy link
Copy Markdown
Member Author

I wrote PR #11830: the other possible fix.

@vstinner

Copy link
Copy Markdown
Member Author

I'm going to merge PR #11830 instead which is safer.

@vstinner vstinner closed this Feb 13, 2019
@vstinner vstinner deleted the slice_richcompare branch February 13, 2019 11:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants