From a742913607bb0665a877c4402d130f6491de3617 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Mon, 22 Jan 2024 11:38:38 +0300 Subject: [PATCH 1/2] gh-114414: Check for `NULL` after `PyType_GetModuleByDef` in `_threadmodule` --- Modules/_threadmodule.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Modules/_threadmodule.c b/Modules/_threadmodule.c index afcf646e3bc19e..c0f0131eb5a6f4 100644 --- a/Modules/_threadmodule.c +++ b/Modules/_threadmodule.c @@ -901,6 +901,9 @@ local_new(PyTypeObject *type, PyObject *args, PyObject *kw) } PyObject *module = PyType_GetModuleByDef(type, &thread_module); + if (module == NULL) { + return NULL; + } thread_module_state *state = get_thread_state(module); localobject *self = (localobject *)type->tp_alloc(type, 0); @@ -1042,6 +1045,9 @@ static int local_setattro(localobject *self, PyObject *name, PyObject *v) { PyObject *module = PyType_GetModuleByDef(Py_TYPE(self), &thread_module); + if (module == NULL) { + return NULL; + } thread_module_state *state = get_thread_state(module); PyObject *ldict = _ldict(self, state); @@ -1094,6 +1100,9 @@ static PyObject * local_getattro(localobject *self, PyObject *name) { PyObject *module = PyType_GetModuleByDef(Py_TYPE(self), &thread_module); + if (module == NULL) { + return NULL; + } thread_module_state *state = get_thread_state(module); PyObject *ldict = _ldict(self, state); From 7669789d54d5c66ac3bf8b2458f12205257c8708 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Mon, 22 Jan 2024 11:51:46 +0300 Subject: [PATCH 2/2] Use `assert` instead --- Modules/_threadmodule.c | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/Modules/_threadmodule.c b/Modules/_threadmodule.c index c0f0131eb5a6f4..99f97eb6d0adcc 100644 --- a/Modules/_threadmodule.c +++ b/Modules/_threadmodule.c @@ -901,9 +901,7 @@ local_new(PyTypeObject *type, PyObject *args, PyObject *kw) } PyObject *module = PyType_GetModuleByDef(type, &thread_module); - if (module == NULL) { - return NULL; - } + assert(module != NULL); thread_module_state *state = get_thread_state(module); localobject *self = (localobject *)type->tp_alloc(type, 0); @@ -1045,9 +1043,7 @@ static int local_setattro(localobject *self, PyObject *name, PyObject *v) { PyObject *module = PyType_GetModuleByDef(Py_TYPE(self), &thread_module); - if (module == NULL) { - return NULL; - } + assert(module != NULL); thread_module_state *state = get_thread_state(module); PyObject *ldict = _ldict(self, state); @@ -1100,9 +1096,7 @@ static PyObject * local_getattro(localobject *self, PyObject *name) { PyObject *module = PyType_GetModuleByDef(Py_TYPE(self), &thread_module); - if (module == NULL) { - return NULL; - } + assert(module != NULL); thread_module_state *state = get_thread_state(module); PyObject *ldict = _ldict(self, state);