]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-127196: Fix crash in `_interpreters`, when `shared` had invalid encodings (#127220)
authorsobolevn <mail@sobolevn.me>
Thu, 9 Jan 2025 19:45:35 +0000 (22:45 +0300)
committerGitHub <noreply@github.com>
Thu, 9 Jan 2025 19:45:35 +0000 (19:45 +0000)
Lib/test/test__interpreters.py
Misc/NEWS.d/next/Library/2024-11-24-14-53-35.gh-issue-127196.8CBkUa.rst [new file with mode: 0644]
Modules/_interpretersmodule.c

index bf3165e2341949f57aeb61637ca13c17425e0846..fd444f1f06ce48077c0a9ceeaefcf29128a3aa38 100644 (file)
@@ -557,7 +557,7 @@ class CommonTests(TestBase):
         self.id = _interpreters.create()
 
     def test_signatures(self):
-        # for method in ['exec', 'run_string', 'run_func']:
+        # See https://github.com/python/cpython/issues/126654
         msg = "expected 'shared' to be a dict"
         with self.assertRaisesRegex(TypeError, msg):
             _interpreters.exec(self.id, 'a', 1)
@@ -568,6 +568,17 @@ class CommonTests(TestBase):
         with self.assertRaisesRegex(TypeError, msg):
             _interpreters.run_func(self.id, lambda: None, shared=1)
 
+    def test_invalid_shared_encoding(self):
+        # See https://github.com/python/cpython/issues/127196
+        bad_shared = {"\uD82A": 0}
+        msg = 'surrogates not allowed'
+        with self.assertRaisesRegex(UnicodeEncodeError, msg):
+            _interpreters.exec(self.id, 'a', shared=bad_shared)
+        with self.assertRaisesRegex(UnicodeEncodeError, msg):
+            _interpreters.run_string(self.id, 'a', shared=bad_shared)
+        with self.assertRaisesRegex(UnicodeEncodeError, msg):
+            _interpreters.run_func(self.id, lambda: None, shared=bad_shared)
+
 
 class RunStringTests(TestBase):
 
diff --git a/Misc/NEWS.d/next/Library/2024-11-24-14-53-35.gh-issue-127196.8CBkUa.rst b/Misc/NEWS.d/next/Library/2024-11-24-14-53-35.gh-issue-127196.8CBkUa.rst
new file mode 100644 (file)
index 0000000..471f64d
--- /dev/null
@@ -0,0 +1,2 @@
+Fix crash when dict with keys in invalid encoding were passed to several
+functions in ``_interpreters`` module.
index a36823c4bb982b4a04253d7a64b69dab47e194d1..fcd0baf696f943c252bb0140261c4119ca93d61e 100644 (file)
@@ -459,7 +459,12 @@ _run_in_interpreter(PyInterpreterState *interp,
 
     // Prep and switch interpreters.
     if (_PyXI_Enter(&session, interp, shareables) < 0) {
-        assert(!PyErr_Occurred());
+        if (PyErr_Occurred()) {
+            // If an error occured at this step, it means that interp
+            // was not prepared and switched.
+            return -1;
+        }
+        // Now, apply the error from another interpreter:
         PyObject *excinfo = _PyXI_ApplyError(session.error);
         if (excinfo != NULL) {
             *p_excinfo = excinfo;