]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.13] gh-150633: Properly handle null characters in the name when importing frozen...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Mon, 8 Jun 2026 18:55:43 +0000 (20:55 +0200)
committerGitHub <noreply@github.com>
Mon, 8 Jun 2026 18:55:43 +0000 (18:55 +0000)
(cherry picked from commit 54de5475cd753e2519692c3e54af0f150e0a8b62)

Co-authored-by: Thomas Kowalski <thom.kowa@gmail.com>
Lib/test/test_import/__init__.py
Misc/NEWS.d/next/Core_and_Builtins/2026-05-30-20-19-35.gh-issue-150633.XkNul0.rst [new file with mode: 0644]
Python/import.c

index 4d2c263bebd2728b2d1759c06614cb5d81c90fbd..4eb1deaf348bbb7c5b407723a961b4b11a690acc 100644 (file)
@@ -379,6 +379,15 @@ class ImportTests(unittest.TestCase, ExtraAssertions):
         with self.assertRaises(ModuleNotFoundError):
             import something_that_should_not_exist_anywhere
 
+    def test_import_null_byte_in_name_raises_ModuleNotFoundError(self):
+        # gh-150633: module names containing null bytes should not
+        # lead to duplicates in sys.modules
+        before = set(sys.modules.keys())
+        with self.assertRaises(ModuleNotFoundError):
+            __import__('zipimport\x00junk')
+
+        self.assertEqual(set(sys.modules.keys()), before)
+
     def test_from_import_missing_module_raises_ModuleNotFoundError(self):
         with self.assertRaises(ModuleNotFoundError):
             from something_that_should_not_exist_anywhere import blah
diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-05-30-20-19-35.gh-issue-150633.XkNul0.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-05-30-20-19-35.gh-issue-150633.XkNul0.rst
new file mode 100644 (file)
index 0000000..c397ad6
--- /dev/null
@@ -0,0 +1,3 @@
+Fix the frozen importer accepting module names with embedded null bytes, which
+caused it to bypass the :data:`sys.modules` cache and create duplicate module
+objects.
index d5d3b79357664bdda6668df1bdc377c17fe2f553..44d0e5420cfe02538a9d6b13b586297b86fca30a 100644 (file)
@@ -2986,7 +2986,7 @@ find_frozen(PyObject *nameobj, struct frozen_info *info)
     if (nameobj == NULL || nameobj == Py_None) {
         return FROZEN_BAD_NAME;
     }
-    const char *name = PyUnicode_AsUTF8(nameobj);
+    const char *name = _PyUnicode_AsUTF8NoNUL(nameobj);
     if (name == NULL) {
         // Note that this function previously used
         // _PyUnicode_EqualToASCIIString().  We clear the error here