diff --git a/Objects/typeobject.c b/Objects/typeobject.c index 006df8d1f0..9e5ddef0c1 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -1003,8 +1003,11 @@ PyObject * PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems) { PyObject *obj; - const size_t size = _PyObject_VAR_SIZE(type, nitems+1); - /* note that we need to add one, for the sentinel */ + size_t extra = 0; + if (type->tp_flags & Py_TPFLAGS_TYPE_SUBCLASS) { + extra = 1; // need a sentinel at end for types + } + const size_t size = _PyObject_VAR_SIZE(type, nitems+extra); if (PyType_IS_GC(type)) obj = _PyObject_GC_Malloc(size);