[Python-checkins] gh-105979: Fix exception handling in `unmarshal_frozen_code` (`Python/import.c`) (#105980)

kumaraditya303 webhook-mailer at python.org
Thu Jun 22 17:30:23 EDT 2023


https://github.com/python/cpython/commit/cd5280367a3a7065d13b8f7234474f7a2e9a18fd
commit: cd5280367a3a7065d13b8f7234474f7a2e9a18fd
branch: main
author: chgnrdv <52372310+chgnrdv at users.noreply.github.com>
committer: kumaraditya303 <59607654+kumaraditya303 at users.noreply.github.com>
date: 2023-06-22T21:30:19Z
summary:

gh-105979: Fix exception handling in `unmarshal_frozen_code` (`Python/import.c`) (#105980)

files:
A Misc/NEWS.d/next/Core and Builtins/2023-06-22-19-16-24.gh-issue-105979.TDP2CU.rst
M Lib/test/test_import/__init__.py
M Python/import.c

diff --git a/Lib/test/test_import/__init__.py b/Lib/test/test_import/__init__.py
index f2726da203ef..e0e2354ae2f1 100644
--- a/Lib/test/test_import/__init__.py
+++ b/Lib/test/test_import/__init__.py
@@ -23,6 +23,7 @@
 import unittest
 from unittest import mock
 import _testinternalcapi
+import _imp
 
 from test.support import os_helper
 from test.support import (
@@ -763,6 +764,13 @@ def test_dll_dependency_import(self):
                                     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
index 000000000000..be6962afd0c7
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2023-06-22-19-16-24.gh-issue-105979.TDP2CU.rst	
@@ -0,0 +1 @@
+Fix crash in :func:`!_imp.get_frozen_object` due to improper exception handling.
diff --git a/Python/import.c b/Python/import.c
index 969902afea1c..9b1ad87b84f6 100644
--- a/Python/import.c
+++ b/Python/import.c
@@ -2071,6 +2071,7 @@ unmarshal_frozen_code(PyInterpreterState *interp, 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;
     }



More information about the Python-checkins mailing list