FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

[3.10] bpo-46417: Fix race condition on setting type __bases__ (GH-30788) by miss-islington · Pull Request #30789 · python/cpython · GitHub

/ cpython Public
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) .rst  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Fix a race condition on setting a type ``__bases__`` attribute: the internal
function ``add_subclass()`` now gets the ``PyTypeObject.tp_subclasses``
member after calling :c:func:`PyWeakref_NewRef` which can trigger a garbage
collection which can indirectly modify ``PyTypeObject.tp_subclasses``. Patch
by Victor Stinner.
27 changes: 16 additions & 11 deletions Objects/typeobject.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -6384,24 +6384,29 @@ PyType_Ready(PyTypeObject *type)
static int
add_subclass(PyTypeObject *base, PyTypeObject *type)
{
int result = -1;
PyObject *dict, *key, *newobj;
PyObject *key = PyLong_FromVoidPtr((void *) type);
if (key == NULL)
return -1;

dict = base->tp_subclasses;
PyObject *ref = PyWeakref_NewRef((PyObject *)type, NULL);
if (ref == NULL) {
Py_DECREF(key);
return -1;
}

// Only get tp_subclasses after creating the key and value.
// PyWeakref_NewRef() can trigger a garbage collection which can execute
// arbitrary Python code and so modify base->tp_subclasses.
PyObject *dict = base->tp_subclasses;
if (dict == NULL) {
base->tp_subclasses = dict = PyDict_New();
if (dict == NULL)
return -1;
}
assert(PyDict_CheckExact(dict));
key = PyLong_FromVoidPtr((void *) type);
if (key == NULL)
return -1;
newobj = PyWeakref_NewRef((PyObject *)type, NULL);
if (newobj != NULL) {
result = PyDict_SetItem(dict, key, newobj);
Py_DECREF(newobj);
}

int result = PyDict_SetItem(dict, key, ref);
Py_DECREF(ref);
Py_DECREF(key);
return result;
}
Expand Down

Back | FazBrowse Home | New Git URL