Skip to content

Commit 706070c

Browse files
[3.14] gh-159739: Keep generic alias arguments alive while formatting errors (GH-159981) (#160021)
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. (cherry picked from commit 9c649db) Co-authored-by: Jelle Zijlstra <jelle@openai.com>
1 parent f3453a0 commit 706070c

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
@@ -546,13 +546,13 @@ _Py_subs_parameters(PyObject *self, PyObject *args, PyObject *parameters, PyObje
546546
}
547547
if (unpack) {
548548
if (!PyTuple_Check(arg)) {
549-
Py_DECREF(newargs);
550-
Py_DECREF(item);
551-
Py_XDECREF(tuple_args);
552549
PyObject *original = PyTuple_GET_ITEM(args, iarg);
553550
PyErr_Format(PyExc_TypeError,
554551
"expected __typing_subst__ of %T objects to return a tuple, not %T",
555552
original, arg);
553+
Py_DECREF(newargs);
554+
Py_DECREF(item);
555+
Py_XDECREF(tuple_args);
556556
Py_DECREF(arg);
557557
return NULL;
558558
}

0 commit comments

Comments
 (0)