Skip to content

Commit 4834b45

Browse files
[3.13] gh-150633: Properly handle null characters in the name when importing frozen modules (GH-150634) (GH-151102)
(cherry picked from commit 54de547) Co-authored-by: Thomas Kowalski <thom.kowa@gmail.com>
1 parent 947aa70 commit 4834b45

3 files changed

Lines changed: 13 additions & 1 deletion

File tree

Lib/test/test_import/__init__.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,15 @@ def test_import_raises_ModuleNotFoundError(self):
379379
with self.assertRaises(ModuleNotFoundError):
380380
import something_that_should_not_exist_anywhere
381381

382+
def test_import_null_byte_in_name_raises_ModuleNotFoundError(self):
383+
# gh-150633: module names containing null bytes should not
384+
# lead to duplicates in sys.modules
385+
before = set(sys.modules.keys())
386+
with self.assertRaises(ModuleNotFoundError):
387+
__import__('zipimport\x00junk')
388+
389+
self.assertEqual(set(sys.modules.keys()), before)
390+
382391
def test_from_import_missing_module_raises_ModuleNotFoundError(self):
383392
with self.assertRaises(ModuleNotFoundError):
384393
from something_that_should_not_exist_anywhere import blah
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix the frozen importer accepting module names with embedded null bytes, which
2+
caused it to bypass the :data:`sys.modules` cache and create duplicate module
3+
objects.

Python/import.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2986,7 +2986,7 @@ find_frozen(PyObject *nameobj, struct frozen_info *info)
29862986
if (nameobj == NULL || nameobj == Py_None) {
29872987
return FROZEN_BAD_NAME;
29882988
}
2989-
const char *name = PyUnicode_AsUTF8(nameobj);
2989+
const char *name = _PyUnicode_AsUTF8NoNUL(nameobj);
29902990
if (name == NULL) {
29912991
// Note that this function previously used
29922992
// _PyUnicode_EqualToASCIIString(). We clear the error here

0 commit comments

Comments
 (0)