Skip to content

Commit 677acec

Browse files
committed
gh-150146: Fix NULL dereference in _Py_subs_parameters
1 parent d948eaa commit 677acec

3 files changed

Lines changed: 18 additions & 3 deletions

File tree

Lib/test/test_genericalias.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,14 @@
5555
from unittest.case import _AssertRaisesContext
5656
from queue import Queue, SimpleQueue
5757
from weakref import WeakSet, ReferenceType, ref
58-
import typing
59-
from typing import Unpack
6058
try:
6159
from tkinter import Event
6260
except ImportError:
6361
Event = None
6462
from string.templatelib import Template, Interpolation
6563

66-
from typing import TypeVar
64+
import typing
65+
from typing import TypeVar, Unpack
6766
T = TypeVar('T')
6867
K = TypeVar('K')
6968
V = TypeVar('V')
@@ -621,6 +620,14 @@ def test_nested_paramspec_specialization(self):
621620
self.assertEqual(deeply_nested_specialized.__args__, ([str, [float], int], float))
622621
self.assertEqual(deeply_nested_specialized.__parameters__, ())
623622

623+
def test_gh150146(self):
624+
# It used to crash:
625+
for container in [memoryview, list, tuple]:
626+
with self.subTest(container=container):
627+
x = container[TypeVar("")]
628+
with self.assertRaises(TypeError):
629+
x[*typing.Mapping[..., ...]]
630+
624631

625632
class TypeIterationTests(unittest.TestCase):
626633
_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
@@ -412,6 +412,9 @@ _Py_subs_parameters(PyObject *self, PyObject *args, PyObject *parameters, PyObje
412412
self);
413413
}
414414
item = _unpack_args(item);
415+
if (item == NULL) {
416+
return NULL;
417+
}
415418
for (Py_ssize_t i = 0; i < nparams; i++) {
416419
PyObject *param = PyTuple_GET_ITEM(parameters, i);
417420
PyObject *prepare, *tmp;

0 commit comments

Comments
 (0)