Skip to content

Commit bfb855b

Browse files
authored
bpo-34784: Implement correct cleanup in PyStructSequence new implementation (GH-10536)
PyTuple_Pack can fail and return NULL. If this happens, then PyType_FromSpecWithBases will incorrectly create a new type without bases. Also, it will crash on the Py_DECREF that follows. Also free members and type in error conditions.
1 parent 4c596d5 commit bfb855b

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

Objects/structseq.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,10 @@ PyStructSequence_NewType(PyStructSequence_Desc *desc)
447447
spec.slots = slots;
448448

449449
bases = PyTuple_Pack(1, &PyTuple_Type);
450+
if (bases == NULL) {
451+
PyMem_FREE(members);
452+
return NULL;
453+
}
450454
type = (PyTypeObject *)PyType_FromSpecWithBases(&spec, bases);
451455
Py_DECREF(bases);
452456
PyMem_FREE(members);
@@ -456,6 +460,7 @@ PyStructSequence_NewType(PyStructSequence_Desc *desc)
456460

457461
if (initialize_structseq_dict(
458462
desc, type->tp_dict, n_members, n_unnamed_members) < 0) {
463+
Py_DECREF(type);
459464
return NULL;
460465
}
461466

0 commit comments

Comments
 (0)