]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-128636: Fix crash in PyREPL when `os.environ` is overwritten with an invalid value...
authorTomas R. <tomas.roun8@gmail.com>
Wed, 22 Jan 2025 16:15:23 +0000 (17:15 +0100)
committerGitHub <noreply@github.com>
Wed, 22 Jan 2025 16:15:23 +0000 (16:15 +0000)
Lib/_pyrepl/unix_console.py
Lib/test/test_pyrepl/test_unix_console.py
Misc/NEWS.d/next/Library/2025-01-08-22-30-38.gh-issue-128636.jQfWXj.rst [new file with mode: 0644]

index add31d52f7865134b90983420a058595fa3e4693..96379bc20f3357056938a3fc1e53207455eebc1f 100644 (file)
@@ -449,10 +449,12 @@ class UnixConsole(Console):
             """
             try:
                 return int(os.environ["LINES"]), int(os.environ["COLUMNS"])
-            except KeyError:
-                height, width = struct.unpack(
-                    "hhhh", ioctl(self.input_fd, TIOCGWINSZ, b"\000" * 8)
-                )[0:2]
+            except (KeyError, TypeError, ValueError):
+                try:
+                    size = ioctl(self.input_fd, TIOCGWINSZ, b"\000" * 8)
+                except OSError:
+                    return 25, 80
+                height, width = struct.unpack("hhhh", size)[0:2]
                 if not height:
                     return 25, 80
                 return height, width
@@ -468,7 +470,7 @@ class UnixConsole(Console):
             """
             try:
                 return int(os.environ["LINES"]), int(os.environ["COLUMNS"])
-            except KeyError:
+            except (KeyError, TypeError, ValueError):
                 return 25, 80
 
     def forgetinput(self):
index e3bbabcb0089fb233290380392fb9cd62393e414..15dbf48bcf0f1c44d396b2cacdd13ec559313bb8 100644 (file)
@@ -1,7 +1,9 @@
 import itertools
+import os
 import sys
 import unittest
 from functools import partial
+from test.support import os_helper
 from unittest import TestCase
 from unittest.mock import MagicMock, call, patch, ANY
 
@@ -312,3 +314,14 @@ class TestConsole(TestCase):
         )
         console.restore()
         con.restore()
+
+    def test_getheightwidth_with_invalid_environ(self, _os_write):
+        # gh-128636
+        console = UnixConsole()
+        with os_helper.EnvironmentVarGuard() as env:
+            env["LINES"] = ""
+            self.assertIsInstance(console.getheightwidth(), tuple)
+            env["COLUMNS"] = ""
+            self.assertIsInstance(console.getheightwidth(), tuple)
+            os.environ = []
+            self.assertIsInstance(console.getheightwidth(), tuple)
diff --git a/Misc/NEWS.d/next/Library/2025-01-08-22-30-38.gh-issue-128636.jQfWXj.rst b/Misc/NEWS.d/next/Library/2025-01-08-22-30-38.gh-issue-128636.jQfWXj.rst
new file mode 100644 (file)
index 0000000..80c9840
--- /dev/null
@@ -0,0 +1,2 @@
+Fix PyREPL failure when :data:`os.environ` is overwritten with an invalid
+value.