Skip to content

Commit 9c649db

Browse files
authored
gh-159739: Keep generic alias arguments alive while formatting errors (#159981)
When a generic alias contains a list of parameters and an object claiming to be an unpacked type-variable tuple returns a non-tuple from `__typing_subst__`, the error path frees the temporary argument tuple before reading it to format a `TypeError`. That use-after-free can crash the interpreter instead of reporting the invalid substitution.
1 parent ebce4b7 commit 9c649db

2 files changed

Lines changed: 4 additions & 3 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix a crash when constructing an error for invalid generic alias arguments.

‎Objects/genericaliasobject.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -550,13 +550,13 @@ _Py_subs_parameters(PyObject *self, PyObject *args, PyObject *parameters, PyObje
550550
}
551551
if (unpack) {
552552
if (!PyTuple_Check(arg)) {
553-
Py_DECREF(newargs);
554-
Py_DECREF(item);
555-
Py_XDECREF(tuple_args);
556553
PyObject *original = PyTuple_GET_ITEM(args, iarg);
557554
PyErr_Format(PyExc_TypeError,
558555
"expected __typing_subst__ of %T objects to return a tuple, not %T",
559556
original, arg);
557+
Py_DECREF(newargs);
558+
Py_DECREF(item);
559+
Py_XDECREF(tuple_args);
560560
Py_DECREF(arg);
561561
return NULL;
562562
}

0 commit comments

Comments
 (0)