Skip to content

Commit 0942455

Browse files
authored
[3.13] gh-154275: Do not crash on deeply nested __parameters__ in GenericAlias (GH-154277) (#154340)
* [3.13] gh-154275: Do not crash on deeply nested `__parameters__` in `GenericAlias` (GH-154277) (cherry picked from commit 1034e07) Co-authored-by: sobolevn <mail@sobolevn.me>
1 parent 2efd2b8 commit 0942455

2 files changed

Lines changed: 20 additions & 9 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix a crash when getting deeply nested ``__parameters__`` from a
2+
:class:`types.GenericAlias` objects.

Objects/genericaliasobject.c

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -244,11 +244,16 @@ tuple_extend(PyObject **dst, Py_ssize_t dstindex,
244244
PyObject *
245245
_Py_make_parameters(PyObject *args)
246246
{
247+
if (Py_EnterRecursiveCall(" in __parameter__ calculation")) {
248+
return NULL;
249+
}
250+
247251
Py_ssize_t nargs = PyTuple_GET_SIZE(args);
248252
Py_ssize_t len = nargs;
249253
PyObject *parameters = PyTuple_New(len);
250-
if (parameters == NULL)
251-
return NULL;
254+
if (parameters == NULL) {
255+
goto cleanup;
256+
}
252257
Py_ssize_t iparam = 0;
253258
for (Py_ssize_t iarg = 0; iarg < nargs; iarg++) {
254259
PyObject *t = PyTuple_GET_ITEM(args, iarg);
@@ -258,8 +263,7 @@ _Py_make_parameters(PyObject *args)
258263
}
259264
int rc = PyObject_HasAttrWithError(t, &_Py_ID(__typing_subst__));
260265
if (rc < 0) {
261-
Py_DECREF(parameters);
262-
return NULL;
266+
goto error;
263267
}
264268
if (rc) {
265269
iparam += tuple_add(parameters, iparam, t);
@@ -268,8 +272,7 @@ _Py_make_parameters(PyObject *args)
268272
PyObject *subparams;
269273
if (PyObject_GetOptionalAttr(t, &_Py_ID(__parameters__),
270274
&subparams) < 0) {
271-
Py_DECREF(parameters);
272-
return NULL;
275+
goto error;
273276
}
274277
if (subparams && PyTuple_Check(subparams)) {
275278
Py_ssize_t len2 = PyTuple_GET_SIZE(subparams);
@@ -278,7 +281,7 @@ _Py_make_parameters(PyObject *args)
278281
len += needed;
279282
if (_PyTuple_Resize(&parameters, len) < 0) {
280283
Py_DECREF(subparams);
281-
return NULL;
284+
goto cleanup;
282285
}
283286
}
284287
for (Py_ssize_t j = 0; j < len2; j++) {
@@ -291,11 +294,17 @@ _Py_make_parameters(PyObject *args)
291294
}
292295
if (iparam < len) {
293296
if (_PyTuple_Resize(&parameters, iparam) < 0) {
294-
Py_XDECREF(parameters);
295-
return NULL;
297+
goto error;
296298
}
297299
}
300+
Py_LeaveRecursiveCall();
298301
return parameters;
302+
303+
error:
304+
Py_XDECREF(parameters);
305+
cleanup:
306+
Py_LeaveRecursiveCall();
307+
return NULL;
299308
}
300309

301310
/* If obj is a generic alias, substitute type variables params

0 commit comments

Comments
 (0)