Skip to content

Commit 0977fc2

Browse files
[3.14] gh-150633: Properly handle null characters in the name when importing frozen modules (GH-150634) (GH-151101)
(cherry picked from commit 54de547) Co-authored-by: Thomas Kowalski <thom.kowa@gmail.com>
1 parent 9e3d9fc commit 0977fc2

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
@@ -360,6 +360,15 @@ def test_import_raises_ModuleNotFoundError(self):
360360
with self.assertRaises(ModuleNotFoundError):
361361
import something_that_should_not_exist_anywhere
362362

363+
def test_import_null_byte_in_name_raises_ModuleNotFoundError(self):
364+
# gh-150633: module names containing null bytes should not
365+
# lead to duplicates in sys.modules
366+
before = set(sys.modules.keys())
367+
with self.assertRaises(ModuleNotFoundError):
368+
__import__('zipimport\x00junk')
369+
370+
self.assertEqual(set(sys.modules.keys()), before)
371+
363372
def test_from_import_missing_module_raises_ModuleNotFoundError(self):
364373
with self.assertRaises(ModuleNotFoundError):
365374
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
@@ -3025,7 +3025,7 @@ find_frozen(PyObject *nameobj, struct frozen_info *info)
30253025
if (nameobj == NULL || nameobj == Py_None) {
30263026
return FROZEN_BAD_NAME;
30273027
}
3028-
const char *name = PyUnicode_AsUTF8(nameobj);
3028+
const char *name = _PyUnicode_AsUTF8NoNUL(nameobj);
30293029
if (name == NULL) {
30303030
// Note that this function previously used
30313031
// _PyUnicode_EqualToASCIIString(). We clear the error here

0 commit comments

Comments
 (0)