[3.14] gh-145187: Fix crash on invalid type parameter bound expression in conditional block (GH-145188) (#145196)

gh-145187: Fix crash on invalid type parameter bound expression in conditional block (GH-145188)

Fix parsing crash found by oss-fuzz
(cherry picked from commit 5e61a16c10)

Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com>
This commit is contained in:
Miss Islington (bot) 2026-02-24 22:13:08 +01:00 committed by GitHub
parent bbce6ba08c
commit 12092af02e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 12 additions and 3 deletions

View file

@ -152,6 +152,13 @@ def test_incorrect_mro_explicit_object(self):
with self.assertRaisesRegex(TypeError, r"\(MRO\) for bases object, Generic"):
class My[X](object): ...
def test_compile_error_in_type_param_bound(self):
# This should not crash, see gh-145187
check_syntax_error(
self,
"if True:\n class h[l:{7for*()in 0}]:2"
)
class TypeParamsNonlocalTest(unittest.TestCase):
def test_nonlocal_disallowed_01(self):

View file

@ -0,0 +1,2 @@
Fix compiler assertion fail when a type parameter bound contains an invalid
expression in a conditional block.

View file

@ -1200,11 +1200,11 @@ codegen_type_param_bound_or_default(compiler *c, expr_ty e,
ADDOP_LOAD_CONST_NEW(c, LOC(e), defaults);
RETURN_IF_ERROR(codegen_setup_annotations_scope(c, LOC(e), key, name));
if (allow_starred && e->kind == Starred_kind) {
VISIT(c, expr, e->v.Starred.value);
ADDOP_I(c, LOC(e), UNPACK_SEQUENCE, (Py_ssize_t)1);
VISIT_IN_SCOPE(c, expr, e->v.Starred.value);
ADDOP_I_IN_SCOPE(c, LOC(e), UNPACK_SEQUENCE, (Py_ssize_t)1);
}
else {
VISIT(c, expr, e);
VISIT_IN_SCOPE(c, expr, e);
}
ADDOP_IN_SCOPE(c, LOC(e), RETURN_VALUE);
PyCodeObject *co = _PyCompile_OptimizeAndAssemble(c, 1);