Skip to content

Commit 6aa59c6

Browse files
bpo-44184: Apply GH-26274 to the non-GC-type branch of subtype_dealloc (GH-27165) (GH-27174)
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 356bdff commit 6aa59c6

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
@@ -1344,14 +1344,22 @@ subtype_dealloc(PyObject *self)
13441344
/* Extract the type again; tp_del may have changed it */
13451345
type = Py_TYPE(self);
13461346

1347+
// Don't read type memory after calling basedealloc() since basedealloc()
1348+
// can deallocate the type and free its memory.
1349+
int type_needs_decref = (type->tp_flags & Py_TPFLAGS_HEAPTYPE
1350+
&& !(base->tp_flags & Py_TPFLAGS_HEAPTYPE));
1351+
13471352
/* Call the base tp_dealloc() */
13481353
assert(basedealloc);
13491354
basedealloc(self);
13501355

1351-
/* Only decref if the base type is not already a heap allocated type.
1352-
Otherwise, basedealloc should have decref'd it already */
1353-
if (type->tp_flags & Py_TPFLAGS_HEAPTYPE && !(base->tp_flags & Py_TPFLAGS_HEAPTYPE))
1356+
/* Can't reference self beyond this point. It's possible tp_del switched
1357+
our type from a HEAPTYPE to a non-HEAPTYPE, so be careful about
1358+
reference counting. Only decref if the base type is not already a heap
1359+
allocated type. Otherwise, basedealloc should have decref'd it already */
1360+
if (type_needs_decref) {
13541361
Py_DECREF(type);
1362+
}
13551363

13561364
/* Done */
13571365
return;

0 commit comments

Comments
 (0)