]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-116520: Fix error handling in `os_get_terminal_size_impl` in `posixmodule` (#116521)
authorNikita Sobolev <mail@sobolevn.me>
Sat, 9 Mar 2024 11:18:38 +0000 (14:18 +0300)
committerGitHub <noreply@github.com>
Sat, 9 Mar 2024 11:18:38 +0000 (14:18 +0300)
Modules/posixmodule.c

index 920a6750f5136d3aacfff2495477d7d422e98a33..940a9cc8955e1104666198e5222361f75f7fe5e9 100644 (file)
@@ -14981,12 +14981,23 @@ os_get_terminal_size_impl(PyObject *module, int fd)
     termsize = PyStructSequence_New((PyTypeObject *)TerminalSizeType);
     if (termsize == NULL)
         return NULL;
-    PyStructSequence_SET_ITEM(termsize, 0, PyLong_FromLong(columns));
-    PyStructSequence_SET_ITEM(termsize, 1, PyLong_FromLong(lines));
-    if (PyErr_Occurred()) {
-        Py_DECREF(termsize);
-        return NULL;
-    }
+
+    int pos = 0;
+
+#define SET_TERMSIZE(CALL)                                   \
+    do {                                                     \
+        PyObject *item = (CALL);                             \
+        if (item == NULL) {                                  \
+            Py_DECREF(termsize);                             \
+            return NULL;                                     \
+        }                                                    \
+        PyStructSequence_SET_ITEM(termsize, pos++, item);    \
+    } while(0)
+
+    SET_TERMSIZE(PyLong_FromLong(columns));
+    SET_TERMSIZE(PyLong_FromLong(lines));
+#undef SET_TERMSIZE
+
     return termsize;
 }
 #endif /* defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) */