Skip to content

Commit 580b03a

Browse files
sobolevnmiss-islington
authored andcommitted
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 bba6c1d commit 580b03a

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')
@@ -619,6 +618,14 @@ def test_nested_paramspec_specialization(self):
619618
self.assertEqual(deeply_nested_specialized.__args__, ([str, [float], int], float))
620619
self.assertEqual(deeply_nested_specialized.__parameters__, ())
621620

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

623630
class TypeIterationTests(unittest.TestCase):
624631
_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
@@ -410,6 +410,9 @@ _Py_subs_parameters(PyObject *self, PyObject *args, PyObject *parameters, PyObje
410410
self);
411411
}
412412
item = _unpack_args(item);
413+
if (item == NULL) {
414+
return NULL;
415+
}
413416
for (Py_ssize_t i = 0; i < nparams; i++) {
414417
PyObject *param = PyTuple_GET_ITEM(parameters, i);
415418
PyObject *prepare, *tmp;

0 commit comments

Comments
 (0)