From 5952605ea2d5329ee2e3cc3400c6b1f40348160d Mon Sep 17 00:00:00 2001 From: Radislav Chugunov Date: Thu, 22 Jun 2023 02:50:26 +0300 Subject: [PATCH 1/4] Fix exception handling in unmarshal_frozen_code (Python/import.c) Clear exception that is set if function fails to unmarshal given data. It will prevent calling 'PyUnicode_FromFormat' in 'set_frozen_error' with exception set --- Python/import.c | 1 + 1 file changed, 1 insertion(+) diff --git a/Python/import.c b/Python/import.c index 969902afea1cd6..9b1ad87b84f649 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; } From 472a6cac676645a85dd0107999bb1ae64b174b1a Mon Sep 17 00:00:00 2001 From: Radislav Chugunov Date: Thu, 22 Jun 2023 03:24:38 +0300 Subject: [PATCH 2/4] added test --- Lib/test/test_import/__init__.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Lib/test/test_import/__init__.py b/Lib/test/test_import/__init__.py index f2726da203efbd..e0e2354ae2f19f 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): From 41f3da7c702dd37e9ad4c4319b265f30f94679ff Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Thu, 22 Jun 2023 19:16:27 +0000 Subject: [PATCH 3/4] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2023-06-22-19-16-24.gh-issue-105979.TDP2CU.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2023-06-22-19-16-24.gh-issue-105979.TDP2CU.rst 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 00000000000000..1f9ef71d6c85fa --- /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. From 3502030787368558aa4aa437e6f5d854a66cc585 Mon Sep 17 00:00:00 2001 From: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> Date: Fri, 23 Jun 2023 02:29:27 +0530 Subject: [PATCH 4/4] Update Misc/NEWS.d/next/Core and Builtins/2023-06-22-19-16-24.gh-issue-105979.TDP2CU.rst --- .../2023-06-22-19-16-24.gh-issue-105979.TDP2CU.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 1f9ef71d6c85fa..be6962afd0c78f 100644 --- 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 @@ -1 +1 @@ -Fix crash in :func:`_imp.get_frozen_object` due to improper exception handling. +Fix crash in :func:`!_imp.get_frozen_object` due to improper exception handling.