From 8c466409546b2b5d942aa6b0ff2578354fc50bb5 Mon Sep 17 00:00:00 2001 From: Petr Viktorin Date: Thu, 16 Oct 2025 10:30:57 +0200 Subject: [PATCH 1/2] gh-139817: Fix refleak in TypeAliasType(qualname=non_string) --- Objects/typevarobject.c | 1 + 1 file changed, 1 insertion(+) diff --git a/Objects/typevarobject.c b/Objects/typevarobject.c index 8a3a1e9834583a..cf48ee4981913b 100644 --- a/Objects/typevarobject.c +++ b/Objects/typevarobject.c @@ -2134,6 +2134,7 @@ typealias_new_impl(PyTypeObject *type, PyObject *name, PyObject *value, } else { if (!PyUnicode_Check(qualname)) { PyErr_SetString(PyExc_TypeError, "qualname must be a string"); + Py_DECREF(module); return NULL; } } From 11d364148a06f1b9d5ae2dca72fad27eab09f6aa Mon Sep 17 00:00:00 2001 From: Petr Viktorin Date: Thu, 16 Oct 2025 12:37:51 +0200 Subject: [PATCH 2/2] Move obtaining module --- Objects/typevarobject.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/Objects/typevarobject.c b/Objects/typevarobject.c index cf48ee4981913b..75a69d4bc3e019 100644 --- a/Objects/typevarobject.c +++ b/Objects/typevarobject.c @@ -2123,22 +2123,21 @@ typealias_new_impl(PyTypeObject *type, PyObject *name, PyObject *value, return NULL; } - PyObject *module = caller(); - if (module == NULL) { - return NULL; - } - if (qualname == NULL || qualname == Py_None) { // If qualname was not set directly, we use name instead. qualname = name; } else { if (!PyUnicode_Check(qualname)) { PyErr_SetString(PyExc_TypeError, "qualname must be a string"); - Py_DECREF(module); return NULL; } } + PyObject *module = caller(); + if (module == NULL) { + return NULL; + } + PyObject *ta = (PyObject *)typealias_alloc( name, qualname, checked_params, NULL, value, module); Py_DECREF(module);