Skip to content

Commit 5751633

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

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
@@ -364,6 +364,15 @@ def test_import_raises_ModuleNotFoundError(self):
364364
with self.assertRaises(ModuleNotFoundError):
365365
import something_that_should_not_exist_anywhere
366366

367+
def test_import_null_byte_in_name_raises_ModuleNotFoundError(self):
368+
# gh-150633: module names containing null bytes should not
369+
# lead to duplicates in sys.modules
370+
before = set(sys.modules.keys())
371+
with self.assertRaises(ModuleNotFoundError):
372+
__import__('zipimport\x00junk')
373+
374+
self.assertEqual(set(sys.modules.keys()), before)
375+
367376
def test_from_import_missing_module_raises_ModuleNotFoundError(self):
368377
with self.assertRaises(ModuleNotFoundError):
369378
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
@@ -3168,7 +3168,7 @@ find_frozen(PyObject *nameobj, struct frozen_info *info)
31683168
if (nameobj == NULL || nameobj == Py_None) {
31693169
return FROZEN_BAD_NAME;
31703170
}
3171-
const char *name = PyUnicode_AsUTF8(nameobj);
3171+
const char *name = _PyUnicode_AsUTF8NoNUL(nameobj);
31723172
if (name == NULL) {
31733173
// Note that this function previously used
31743174
// _PyUnicode_EqualToASCIIString(). We clear the error here

0 commit comments

Comments
 (0)