Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix a heap use-after-free in :mod:`pickle`\'s ``whichmodule`` on
free-threaded builds. Patch by Thomas Kowalski.
20 changes: 16 additions & 4 deletions Modules/_pickle.c
Original file line number Diff line number Diff line change
Expand Up @@ -2055,22 +2055,34 @@ whichmodule(PickleState *st, PyObject *global, PyObject *global_name, PyObject *
return NULL;
}
if (PyDict_CheckExact(modules)) {
PyObject *found_name = NULL;
int error = 0;
i = 0;
Py_BEGIN_CRITICAL_SECTION(modules);
while (PyDict_Next(modules, &i, &module_name, &module)) {
Py_INCREF(module_name);
Py_INCREF(module);
if (_checkmodule(module_name, module, global, dotted_path) == 0) {
Py_DECREF(module);
Py_DECREF(modules);
return module_name;
found_name = module_name;
break;
}
Py_DECREF(module);
Py_DECREF(module_name);
if (PyErr_Occurred()) {
Py_DECREF(modules);
return NULL;
error = 1;
break;
}
}
Py_END_CRITICAL_SECTION();
if (error) {
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.

nitpick: i would prefer checking for errors before checking if (found_name != NULL).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks for taking a look! Updated it.

Py_DECREF(modules);
return NULL;
}
if (found_name != NULL) {
Py_DECREF(modules);
return found_name;
}
}
else {
PyObject *iterator = PyObject_GetIter(modules);
Expand Down
Loading