Skip to content

Commit 6366ff4

Browse files
committed
[3.13] gh-150146: Fix NULL dereference in _Py_subs_parameters (GH-150147)
(cherry picked from commit f621ba1) Co-authored-by: sobolevn <mail@sobolevn.me>
1 parent e87baa8 commit 6366ff4

3 files changed

Lines changed: 16 additions & 2 deletions

File tree

Lib/test/test_genericalias.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,8 @@
5656
from queue import Queue, SimpleQueue
5757
from weakref import WeakSet, ReferenceType, ref
5858
import typing
59-
from typing import Unpack
59+
from typing import TypeVar, Unpack
6060

61-
from typing import TypeVar
6261
T = TypeVar('T')
6362
K = TypeVar('K')
6463
V = TypeVar('V')
@@ -537,6 +536,13 @@ def test_del_iter(self):
537536
iter_x = iter(t)
538537
del iter_x
539538

539+
def test_gh150146(self):
540+
# It used to crash:
541+
for container in [memoryview, list, tuple]:
542+
with self.subTest(container=container):
543+
x = container[TypeVar("")]
544+
with self.assertRaises(TypeError):
545+
x[*typing.Mapping[..., ...]]
540546

541547
class TypeIterationTests(unittest.TestCase):
542548
_UNITERABLE_TYPES = (list, tuple)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Fix a crash on a complex type variable substitution.
2+
3+
``from typing import TypeVar; memoryview[TypeVar("")][*typing.Mapping[...,
4+
...]]`` used to fail due to missing ``NULL`` check on ``_unpack_args`` C
5+
function call.

Objects/genericaliasobject.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,9 @@ _Py_subs_parameters(PyObject *self, PyObject *args, PyObject *parameters, PyObje
443443
self);
444444
}
445445
item = _unpack_args(item);
446+
if (item == NULL) {
447+
return NULL;
448+
}
446449
for (Py_ssize_t i = 0; i < nparams; i++) {
447450
PyObject *param = PyTuple_GET_ITEM(parameters, i);
448451
PyObject *prepare, *tmp;

0 commit comments

Comments
 (0)