Skip to content

Commit 074e765

Browse files
authored
bpo-44184: Apply GH-26274 to the non-GC-type branch of subtype_dealloc (GH-27165)
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
1 parent 82b218f commit 074e765

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

1371+
// Don't read type memory after calling basedealloc() since basedealloc()
1372+
// can deallocate the type and free its memory.
1373+
int type_needs_decref = (type->tp_flags & Py_TPFLAGS_HEAPTYPE
1374+
&& !(base->tp_flags & Py_TPFLAGS_HEAPTYPE));
1375+
13711376
/* Call the base tp_dealloc() */
13721377
assert(basedealloc);
13731378
basedealloc(self);
13741379

1375-
/* Only decref if the base type is not already a heap allocated type.
1376-
Otherwise, basedealloc should have decref'd it already */
1377-
if (type->tp_flags & Py_TPFLAGS_HEAPTYPE && !(base->tp_flags & Py_TPFLAGS_HEAPTYPE))
1380+
/* Can't reference self beyond this point. It's possible tp_del switched
1381+
our type from a HEAPTYPE to a non-HEAPTYPE, so be careful about
1382+
reference counting. Only decref if the base type is not already a heap
1383+
allocated type. Otherwise, basedealloc should have decref'd it already */
1384+
if (type_needs_decref) {
13781385
Py_DECREF(type);
1386+
}
13791387

13801388
/* Done */
13811389
return;

0 commit comments

Comments
 (0)