From 113df89b82b64e5824ba1fb6c3154d132278f7ff Mon Sep 17 00:00:00 2001 From: Okiemute Date: Wed, 18 Mar 2026 16:35:06 -0700 Subject: [PATCH] gh-146091: Fix NULL check in termios.tcsetwinsize Add proper NULL checks for PySequence_GetItem results before passing to PyLong_AsLong. This prevents SystemError when a BadSequence raises TypeError in __getitem__. --- .../2026-03-18-16-58-17.gh-issue-146091.lBbo1L.rst | 3 +++ Modules/termios.c | 13 +++++++++---- 2 files changed, 12 insertions(+), 4 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2026-03-18-16-58-17.gh-issue-146091.lBbo1L.rst diff --git a/Misc/NEWS.d/next/Library/2026-03-18-16-58-17.gh-issue-146091.lBbo1L.rst b/Misc/NEWS.d/next/Library/2026-03-18-16-58-17.gh-issue-146091.lBbo1L.rst new file mode 100644 index 00000000000000..2ed3ea8f90e961 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-03-18-16-58-17.gh-issue-146091.lBbo1L.rst @@ -0,0 +1,3 @@ +Fix a bug in :func:`termios.tcsetwinsize` where passing a sequence that +raises an exception in ``__getitem__`` would cause a :exc:`SystemError` +instead of propagating the original exception. diff --git a/Modules/termios.c b/Modules/termios.c index b4eb06cf8ae8ac..95b9c920f39c12 100644 --- a/Modules/termios.c +++ b/Modules/termios.c @@ -500,19 +500,24 @@ termios_tcsetwinsize_impl(PyObject *module, int fd, PyObject *winsz) PyObject *tmp_item; long winsz_0, winsz_1; tmp_item = PySequence_GetItem(winsz, 0); + if (tmp_item == NULL) { + return NULL; + } winsz_0 = PyLong_AsLong(tmp_item); + Py_DECREF(tmp_item); if (winsz_0 == -1 && PyErr_Occurred()) { - Py_XDECREF(tmp_item); return NULL; } - Py_XDECREF(tmp_item); tmp_item = PySequence_GetItem(winsz, 1); + if (tmp_item == NULL) { + return NULL; + } winsz_1 = PyLong_AsLong(tmp_item); + Py_DECREF(tmp_item); if (winsz_1 == -1 && PyErr_Occurred()) { - Py_XDECREF(tmp_item); return NULL; } - Py_XDECREF(tmp_item); + termiosmodulestate *state = PyModule_GetState(module);