Skip to content

Conversation

yoney
Copy link
Contributor

@yoney yoney commented Oct 19, 2025

Added a critical section to protect the candidates list in the free-threading build, addressing two possible issues below:

  • In the free-threading build, PyList_GetItem() could fail and return NULL. Passing it to PyUnicode_Check() results in UB in the loop below.

for (Py_ssize_t i = 0; i < size; ++i) {
PyObject *elem = PyList_GetItem(candidates, i);
if (!PyUnicode_Check(elem)) {

  • Another possible issue in the free-threading build was the loop below in _Py_CalculateSuggestions().

for (Py_ssize_t i = 0; i < dir_size; ++i) {
PyObject *item = PyList_GET_ITEM(dir, i);
if (_PyUnicode_Equal(name, item)) {
continue;

cc: @mpage @colesbury

Note: I considered placing the critical section inside _Py_CalculateSuggestions(). However, due to the loop in _suggestions__generate_suggestions_impl(), the critical section needs to be earlier. I also checked other places where _Py_CalculateSuggestions() is called, and all of them use local list (candidates) variables in C code, so they are safe.

@yoney yoney marked this pull request as ready for review October 19, 2025 04:44
Py_ssize_t size = PyList_Size(candidates);
for (Py_ssize_t i = 0; i < size; ++i) {
PyObject *elem = PyList_GetItem(candidates, i);
PyObject *elem = PyList_GET_ITEM(candidates, i);
Copy link
Member

Choose a reason for hiding this comment

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

How about using PyList_GetItemRef with this change.

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.

2 participants