Skip to content

Commit 0b47049

Browse files
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 074e765) Co-authored-by: T. Wouters <[email protected]>
1 parent 95596d5 commit 0b47049

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

Objects/typeobject.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1231,14 +1231,22 @@ subtype_dealloc(PyObject *self)
12311231
/* Extract the type again; tp_del may have changed it */
12321232
type = Py_TYPE(self);
12331233

1234+
// Don't read type memory after calling basedealloc() since basedealloc()
1235+
// can deallocate the type and free its memory.
1236+
int type_needs_decref = (type->tp_flags & Py_TPFLAGS_HEAPTYPE
1237+
&& !(base->tp_flags & Py_TPFLAGS_HEAPTYPE));
1238+
12341239
/* Call the base tp_dealloc() */
12351240
assert(basedealloc);
12361241
basedealloc(self);
12371242

1238-
/* Only decref if the base type is not already a heap allocated type.
1239-
Otherwise, basedealloc should have decref'd it already */
1240-
if (type->tp_flags & Py_TPFLAGS_HEAPTYPE && !(base->tp_flags & Py_TPFLAGS_HEAPTYPE))
1243+
/* Can't reference self beyond this point. It's possible tp_del switched
1244+
our type from a HEAPTYPE to a non-HEAPTYPE, so be careful about
1245+
reference counting. Only decref if the base type is not already a heap
1246+
allocated type. Otherwise, basedealloc should have decref'd it already */
1247+
if (type_needs_decref) {
12411248
Py_DECREF(type);
1249+
}
12421250

12431251
/* Done */
12441252
return;

0 commit comments

Comments
 (0)