From 92b1e83c8d8f5f7d1acecf213f97dcf3e8d7a19e Mon Sep 17 00:00:00 2001 From: Stan Ulbrych Date: Sat, 24 Oct 2026 19:07:34 +0100 Subject: [PATCH 1/2] Fix ignored error returns in `sys` initialization, `_ctypes` and `_interpchannels` --- Modules/_ctypes/_ctypes.c | 26 +++++++++++++++++--------- Modules/_interpchannelsmodule.c | 2 +- Python/pylifecycle.c | 20 ++++++++++++++++---- Python/sysmodule.c | 8 ++++++-- 4 files changed, 40 insertions(+), 16 deletions(-) diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c index a2e5cbab10944b8..1035c91005759df 100644 --- a/Modules/_ctypes/_ctypes.c +++ b/Modules/_ctypes/_ctypes.c @@ -2479,22 +2479,30 @@ PyCSimpleType_init(PyObject *self, PyObject *args, PyObject *kwds) } assert(sw_info); #ifdef WORDS_BIGENDIAN - PyObject_SetAttrString(self, "__ctype_le__", swapped); - PyObject_SetAttrString(self, "__ctype_be__", self); - PyObject_SetAttrString(swapped, "__ctype_be__", self); - PyObject_SetAttrString(swapped, "__ctype_le__", swapped); + if (PyObject_SetAttrString(self, "__ctype_le__", swapped) < 0 || + PyObject_SetAttrString(self, "__ctype_be__", self) < 0 || + PyObject_SetAttrString(swapped, "__ctype_be__", self) < 0 || + PyObject_SetAttrString(swapped, "__ctype_le__", swapped) < 0) + { + Py_DECREF(swapped); + return -1; + } /* We are creating the type for the OTHER endian */ sw_info->format = _ctypes_alloc_format_string("<", stginfo->format+1); #else - PyObject_SetAttrString(self, "__ctype_be__", swapped); - PyObject_SetAttrString(self, "__ctype_le__", self); - PyObject_SetAttrString(swapped, "__ctype_le__", self); - PyObject_SetAttrString(swapped, "__ctype_be__", swapped); + if (PyObject_SetAttrString(self, "__ctype_be__", swapped) < 0 || + PyObject_SetAttrString(self, "__ctype_le__", self) < 0 || + PyObject_SetAttrString(swapped, "__ctype_le__", self) < 0 || + PyObject_SetAttrString(swapped, "__ctype_be__", swapped) < 0) + { + Py_DECREF(swapped); + return -1; + } /* We are creating the type for the OTHER endian */ sw_info->format = _ctypes_alloc_format_string(">", stginfo->format+1); #endif Py_DECREF(swapped); - if (PyErr_Occurred()) { + if (sw_info->format == NULL) { return -1; } }; diff --git a/Modules/_interpchannelsmodule.c b/Modules/_interpchannelsmodule.c index 18f892406d10b99..930d7276a3ed45e 100644 --- a/Modules/_interpchannelsmodule.c +++ b/Modules/_interpchannelsmodule.c @@ -2638,7 +2638,7 @@ _channelid_shared(PyThreadState *tstate, PyObject *obj, _PyXIData_t *data) if (st == NULL) { return -1; } - if (!PyObject_IsInstance(obj, (PyObject *)st->ChannelIDType)) { + if (!PyObject_TypeCheck(obj, st->ChannelIDType)) { PyErr_Format(PyExc_TypeError, "'_id' attribute must be an %N object, not %T", st->ChannelIDType, obj); diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index 8888f523559123a..5889e146c055091 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -3211,8 +3211,14 @@ init_sys_streams(PyThreadState *tstate) config->stdio_errors); if (std == NULL) goto error; - PySys_SetObject("__stdin__", std); - _PySys_SetAttr(&_Py_ID(stdin), std); + if (PySys_SetObject("__stdin__", std) < 0) { + Py_DECREF(std); + goto error; + } + if (_PySys_SetAttr(&_Py_ID(stdin), std) < 0) { + Py_DECREF(std); + goto error; + } Py_DECREF(std); /* Set sys.stdout */ @@ -3222,8 +3228,14 @@ init_sys_streams(PyThreadState *tstate) config->stdio_errors); if (std == NULL) goto error; - PySys_SetObject("__stdout__", std); - _PySys_SetAttr(&_Py_ID(stdout), std); + if (PySys_SetObject("__stdout__", std) < 0) { + Py_DECREF(std); + goto error; + } + if (_PySys_SetAttr(&_Py_ID(stdout), std) < 0) { + Py_DECREF(std); + goto error; + } Py_DECREF(std); #if 1 /* Disable this if you have trouble debugging bootstrap stuff */ diff --git a/Python/sysmodule.c b/Python/sysmodule.c index ce75027f67e9f4f..99ab232732123fe 100644 --- a/Python/sysmodule.c +++ b/Python/sysmodule.c @@ -4380,8 +4380,12 @@ _PySys_Create(PyThreadState *tstate, PyObject **sysmod_p) PyUnstable_Module_SetGIL(sysmod, Py_MOD_GIL_NOT_USED); #endif - PyModule_AddIntMacro(sysmod, EXIT_SUCCESS); - PyModule_AddIntMacro(sysmod, EXIT_FAILURE); + if (Pyodule_AddIntMacro(sysmod, EXIT_SUCCESS) < 0) { + goto error; + } + if (PyModule_AddIntMacro(sysmod, EXIT_FAILURE) < 0) { + goto error; + } PyObject *sysdict = PyModule_GetDict(sysmod); if (sysdict == NULL) { From 26d524e9fda296ab9b50f2771db0b6a48ddbc851 Mon Sep 17 00:00:00 2001 From: Stan Ulbrych Date: Sat, 24 Oct 2026 19:45:05 +0100 Subject: [PATCH 2/2] Fix a typo --- Python/sysmodule.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Python/sysmodule.c b/Python/sysmodule.c index 99ab232732123fe..6f336dac058379e 100644 --- a/Python/sysmodule.c +++ b/Python/sysmodule.c @@ -4380,7 +4380,7 @@ _PySys_Create(PyThreadState *tstate, PyObject **sysmod_p) PyUnstable_Module_SetGIL(sysmod, Py_MOD_GIL_NOT_USED); #endif - if (Pyodule_AddIntMacro(sysmod, EXIT_SUCCESS) < 0) { + if (PyModule_AddIntMacro(sysmod, EXIT_SUCCESS) < 0) { goto error; } if (PyModule_AddIntMacro(sysmod, EXIT_FAILURE) < 0) {