]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.11] gh-105979: Fix exception handling in unmarshal_frozen_code (`Python/import...
authorKumar Aditya <59607654+kumaraditya303@users.noreply.github.com>
Mon, 26 Jun 2023 09:00:51 +0000 (14:30 +0530)
committerGitHub <noreply@github.com>
Mon, 26 Jun 2023 09:00:51 +0000 (09:00 +0000)
* [3.11] gh-105979: Fix exception handling in `unmarshal_frozen_code` (`Python/import.c`) (GH-105980).
(cherry picked from commit cd5280367a3a7065d13b8f7234474f7a2e9a18fd)

Co-authored-by: chgnrdv <52372310+chgnrdv@users.noreply.github.com>
Lib/test/test_import/__init__.py
Misc/NEWS.d/next/Core and Builtins/2023-06-22-19-16-24.gh-issue-105979.TDP2CU.rst [new file with mode: 0644]
Python/import.c

index 6c5b80bcee6c24a45e6d8687b0872830d72f0a86..6e2606cdb05bc6d7bd65f67faa5bd61f8cc9791f 100644 (file)
@@ -17,6 +17,7 @@ import threading
 import time
 import unittest
 from unittest import mock
+import _imp
 
 from test.support import os_helper
 from test.support import (
@@ -529,6 +530,13 @@ class ImportTests(unittest.TestCase):
                                     env=env,
                                     cwd=os.path.dirname(pyexe))
 
+    def test_issue105979(self):
+        # this used to crash
+        with self.assertRaises(ImportError) as cm:
+            _imp.get_frozen_object("x", b"6\'\xd5Cu\x12")
+        self.assertIn("Frozen object named 'x' is invalid",
+                      str(cm.exception))
+
 
 @skip_if_dont_write_bytecode
 class FilePermissionTests(unittest.TestCase):
diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-06-22-19-16-24.gh-issue-105979.TDP2CU.rst b/Misc/NEWS.d/next/Core and Builtins/2023-06-22-19-16-24.gh-issue-105979.TDP2CU.rst
new file mode 100644 (file)
index 0000000..be6962a
--- /dev/null
@@ -0,0 +1 @@
+Fix crash in :func:`!_imp.get_frozen_object` due to improper exception handling.
index 07a8b9009290f4f8b61a00c07a3a5ea6636b036a..38c23e02cec6a217f28da5ee374f597bf05ebb8e 100644 (file)
@@ -1327,6 +1327,7 @@ unmarshal_frozen_code(struct frozen_info *info)
     PyObject *co = PyMarshal_ReadObjectFromString(info->data, info->size);
     if (co == NULL) {
         /* Does not contain executable code. */
+        PyErr_Clear();
         set_frozen_error(FROZEN_INVALID, info->nameobj);
         return NULL;
     }