Skip to content
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

Don't use PyList.get_item_unchecked() on free-threaded build #4539

Merged
merged 2 commits into from
Sep 30, 2024

Conversation

ngoldbaum
Copy link
Contributor

Followup for #4410.

get_item_unchecked allows possible access of dangling pointers and other data races because PyList_GET_ITEM returns a borrowed reference. I added bindings for PyList_GetItemRef in #4410 but missed that the liter iterator uses get_item_unchecked.

I could leave the APIs visible since they're already marked as unsafe, but since this API is already cfg-ed out for the limited API I thought it might make sense to disable it for free-threaded build as well.

Will add a release note if we decide disabling it is the correct thing to do.

Copy link
Member

@davidhewitt davidhewitt left a comment

Choose a reason for hiding this comment

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

Thanks, after a bit of examination here I think that disabling this API is the right approach for now.

There's no way to guarantee that the list length remains valid from time of check to time of calling .get_item_unchecked . So the safety invariant cannot realistically be met.

src/types/list.rs Show resolved Hide resolved
@ngoldbaum ngoldbaum force-pushed the list-get-item-unchecked branch from 5c76da7 to 763a241 Compare September 26, 2024 16:46
@ngoldbaum
Copy link
Contributor Author

I also double-checked, and PyObject_GetItem, does apply a critical section (eventually, inside the implementation of getitem for PyList).

Copy link
Member

@davidhewitt davidhewitt left a comment

Choose a reason for hiding this comment

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

Makes sense, thanks for being so thorough here!

@davidhewitt davidhewitt added this pull request to the merge queue Sep 30, 2024
Merged via the queue into PyO3:main with commit ce18f79 Sep 30, 2024
43 checks passed
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.

2 participants