]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.11] gh-116520: Fix error handling in `os_get_terminal_size_impl` in `posixmodule...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Sat, 9 Mar 2024 11:41:46 +0000 (12:41 +0100)
committerGitHub <noreply@github.com>
Sat, 9 Mar 2024 11:41:46 +0000 (11:41 +0000)
gh-116520: Fix error handling in `os_get_terminal_size_impl` in `posixmodule` (GH-116521)
(cherry picked from commit b4b4e764a798bab60324871074ce4cdebb9d01bb)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
Modules/posixmodule.c

index db41e81463ccd43d560d66b5ac55835c792dc53d..b318911d44691ad506305c7a313d5baa79457d99 100644 (file)
@@ -13447,12 +13447,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) */