From e7a2b682ea8de342920f181d414e574288467e86 Mon Sep 17 00:00:00 2001 From: chgnrdv <52372310+chgnrdv@users.noreply.github.com> Date: Fri, 23 Jun 2023 00:30:19 +0300 Subject: [PATCH 1/2] gh-105979: Fix exception handling in `unmarshal_frozen_code` (`Python/import.c`) (#105980) (cherry picked from commit cd5280367a3a7065d13b8f7234474f7a2e9a18fd) --- Lib/test/test_import/__init__.py | 8 ++++++++ .../2023-06-22-19-16-24.gh-issue-105979.TDP2CU.rst | 1 + Python/import.c | 1 + 3 files changed, 10 insertions(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2023-06-22-19-16-24.gh-issue-105979.TDP2CU.rst diff --git a/Lib/test/test_import/__init__.py b/Lib/test/test_import/__init__.py index 6c5b80bcee6c24..6e2606cdb05bc6 100644 --- a/Lib/test/test_import/__init__.py +++ b/Lib/test/test_import/__init__.py @@ -17,6 +17,7 @@ import time import unittest from unittest import mock +import _imp from test.support import os_helper from test.support import ( @@ -529,6 +530,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 00000000000000..be6962afd0c78f --- /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 07a8b9009290f4..38c23e02cec6a2 100644 --- a/Python/import.c +++ b/Python/import.c @@ -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; } From a87da519b4519718a884e00a408c498fb36a9e5c Mon Sep 17 00:00:00 2001 From: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> Date: Mon, 26 Jun 2023 08:26:40 +0000 Subject: [PATCH 2/2] [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>