diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2022-01-22 14:28:36 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-01-22 14:28:36 (GMT) |
commit | acda9f3b90c33e4020237cb9e5c676efb38f7847 (patch) | |
tree | b4654f7a79ca37972c4472869c3adfa9344b30ac /Objects | |
parent | 6111d5dee2b24916ff95dba56efc569396a31851 (diff) | |
download | cpython-acda9f3b90c33e4020237cb9e5c676efb38f7847.zip cpython-acda9f3b90c33e4020237cb9e5c676efb38f7847.tar.gz cpython-acda9f3b90c33e4020237cb9e5c676efb38f7847.tar.bz2 |
bpo-46417: Fix race condition on setting type __bases__ (GH-30788) (GH-30789)
Fix a race condition on setting a type __bases__ attribute: the
internal function add_subclass() now gets the
PyTypeObject.tp_subclasses member after calling PyWeakref_NewRef()
which can trigger a garbage collection which can indirectly modify
PyTypeObject.tp_subclasses.
(cherry picked from commit f1c6ae3270913e095d24ae13ecf96f5a32c8c503)
Co-authored-by: Victor Stinner <vstinner@python.org>
Co-authored-by: Victor Stinner <vstinner@python.org>
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/typeobject.c | 27 |
1 files changed, 16 insertions, 11 deletions
diff --git a/Objects/typeobject.c b/Objects/typeobject.c index b23e36a..10b69fe 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -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; } |