From b905115f5dbf570cf8c2fc4bccab84a2ae6c4e4b Mon Sep 17 00:00:00 2001 From: sobolevn Date: Tue, 3 Oct 2023 10:33:35 +0300 Subject: [PATCH 1/2] gh-110260: Check for `PyList_SetItem` errors in `termios` module --- Modules/termios.c | 48 ++++++++++++++++++++++++++++++----------------- 1 file changed, 31 insertions(+), 17 deletions(-) diff --git a/Modules/termios.c b/Modules/termios.c index c779a757e4fa9b..9d32b629880e7b 100644 --- a/Modules/termios.c +++ b/Modules/termios.c @@ -120,7 +120,7 @@ termios_tcgetattr_impl(PyObject *module, int fd) v = PyBytes_FromStringAndSize(&ch, 1); if (v == NULL) goto err; - PyList_SetItem(cc, i, v); + PyList_SET_ITEM(cc, i, v); } /* Convert the MIN and TIME slots to integer. On some systems, the @@ -128,29 +128,43 @@ termios_tcgetattr_impl(PyObject *module, int fd) only do this in noncanonical input mode. */ if ((mode.c_lflag & ICANON) == 0) { v = PyLong_FromLong((long)mode.c_cc[VMIN]); - if (v == NULL) + if (v == NULL) { + goto err; + } + if (PyList_SetItem(cc, VMIN, v) < 0) { goto err; - PyList_SetItem(cc, VMIN, v); + }; v = PyLong_FromLong((long)mode.c_cc[VTIME]); - if (v == NULL) + if (v == NULL) { + goto err; + } + if (PyList_SetItem(cc, VTIME, v) < 0) { goto err; - PyList_SetItem(cc, VTIME, v); + } } - if (!(v = PyList_New(7))) - goto err; - - PyList_SetItem(v, 0, PyLong_FromLong((long)mode.c_iflag)); - PyList_SetItem(v, 1, PyLong_FromLong((long)mode.c_oflag)); - PyList_SetItem(v, 2, PyLong_FromLong((long)mode.c_cflag)); - PyList_SetItem(v, 3, PyLong_FromLong((long)mode.c_lflag)); - PyList_SetItem(v, 4, PyLong_FromLong((long)ispeed)); - PyList_SetItem(v, 5, PyLong_FromLong((long)ospeed)); - if (PyErr_Occurred()) { - Py_DECREF(v); + if (!(v = PyList_New(7))) { goto err; } - PyList_SetItem(v, 6, cc); + +#define ADD_LONG_ITEM(index, val) \ + do { \ + PyObject *l = PyLong_FromLong((long)val); \ + if (l == NULL) { \ + goto err; \ + } \ + PyList_SET_ITEM(v, index, l); \ + } while (0) + + ADD_LONG_ITEM(0, mode.c_iflag); + ADD_LONG_ITEM(1, mode.c_oflag); + ADD_LONG_ITEM(2, mode.c_cflag); + ADD_LONG_ITEM(3, mode.c_lflag); + ADD_LONG_ITEM(4, ispeed); + ADD_LONG_ITEM(5, ospeed); +#undef ADD_LONG_ITEM + + PyList_SET_ITEM(v, 6, cc); return v; err: Py_DECREF(cc); From 2f4d14444d6c0e878e87a85ff37d0271c0a7c339 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Tue, 3 Oct 2023 13:11:40 +0300 Subject: [PATCH 2/2] Address review --- Modules/termios.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Modules/termios.c b/Modules/termios.c index 9d32b629880e7b..6f07c93bcdb6b5 100644 --- a/Modules/termios.c +++ b/Modules/termios.c @@ -133,7 +133,7 @@ termios_tcgetattr_impl(PyObject *module, int fd) } if (PyList_SetItem(cc, VMIN, v) < 0) { goto err; - }; + } v = PyLong_FromLong((long)mode.c_cc[VTIME]); if (v == NULL) { goto err; @@ -151,6 +151,7 @@ termios_tcgetattr_impl(PyObject *module, int fd) do { \ PyObject *l = PyLong_FromLong((long)val); \ if (l == NULL) { \ + Py_DECREF(v); \ goto err; \ } \ PyList_SET_ITEM(v, index, l); \