summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2021-07-15 23:35:25 (GMT)
committerGitHub <noreply@github.com>2021-07-15 23:35:25 (GMT)
commit0b4704973dbef712d05bdd62349bb4244f545430 (patch)
tree582df4c6783c647aebd35f378c3f5f06f21421ea /Objects
parent95596d5921eeab9ae49f0dc31263a249013b6849 (diff)
downloadcpython-0b4704973dbef712d05bdd62349bb4244f545430.zip
cpython-0b4704973dbef712d05bdd62349bb4244f545430.tar.gz
cpython-0b4704973dbef712d05bdd62349bb4244f545430.tar.bz2
bpo-44184: Apply GH-26274 to the non-GC-type branch of subtype_dealloc (GH-27165) (GH-27175)
The non-GC-type branch of subtype_dealloc is using the type of an object after freeing in the same unsafe way as GH-26274 fixes. (I believe the old news entry covers this change well enough.) https://bugs.python.org/issue44184 (cherry picked from commit 074e7659f208051b6b973f7fdb654dd22b93aaa2) Co-authored-by: T. Wouters <thomas@python.org>
Diffstat (limited to 'Objects')
-rw-r--r--Objects/typeobject.c14
1 files changed, 11 insertions, 3 deletions
diff --git a/Objects/typeobject.c b/Objects/typeobject.c
index acbe3fa..82faa79 100644
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -1231,14 +1231,22 @@ subtype_dealloc(PyObject *self)
/* Extract the type again; tp_del may have changed it */
type = Py_TYPE(self);
+ // Don't read type memory after calling basedealloc() since basedealloc()
+ // can deallocate the type and free its memory.
+ int type_needs_decref = (type->tp_flags & Py_TPFLAGS_HEAPTYPE
+ && !(base->tp_flags & Py_TPFLAGS_HEAPTYPE));
+
/* Call the base tp_dealloc() */
assert(basedealloc);
basedealloc(self);
- /* Only decref if the base type is not already a heap allocated type.
- Otherwise, basedealloc should have decref'd it already */
- if (type->tp_flags & Py_TPFLAGS_HEAPTYPE && !(base->tp_flags & Py_TPFLAGS_HEAPTYPE))
+ /* Can't reference self beyond this point. It's possible tp_del switched
+ our type from a HEAPTYPE to a non-HEAPTYPE, so be careful about
+ reference counting. Only decref if the base type is not already a heap
+ allocated type. Otherwise, basedealloc should have decref'd it already */
+ if (type_needs_decref) {
Py_DECREF(type);
+ }
/* Done */
return;