Skip to content

Commit a0672e0

Browse files
committed
pythongh-127208: Reject null character in _imp.create_dynamic()
_imp.create_dynamic() now rejects embedded null characters in the path and in the module name.
1 parent 762c603 commit a0672e0

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

Lib/test/test_import/__init__.py

+13
Original file line numberDiff line numberDiff line change
@@ -1133,6 +1133,19 @@ def test_script_shadowing_stdlib_sys_path_modification(self):
11331133
stdout, stderr = popen.communicate()
11341134
self.assertRegex(stdout, expected_error)
11351135

1136+
def test_create_dynamic_null(self):
1137+
with self.assertRaisesRegex(ValueError, 'embedded null character'):
1138+
class Spec:
1139+
name = "a\x00b"
1140+
origin = "abc"
1141+
_imp.create_dynamic(Spec())
1142+
1143+
with self.assertRaisesRegex(ValueError, 'embedded null character'):
1144+
class Spec2:
1145+
name = "abc"
1146+
origin = "a\x00b"
1147+
_imp.create_dynamic(Spec2())
1148+
11361149

11371150
@skip_if_dont_write_bytecode
11381151
class FilePermissionTests(unittest.TestCase):

Python/import.c

+5-3
Original file line numberDiff line numberDiff line change
@@ -1157,12 +1157,14 @@ del_extensions_cache_value(struct extensions_cache_value *value)
11571157
static void *
11581158
hashtable_key_from_2_strings(PyObject *str1, PyObject *str2, const char sep)
11591159
{
1160-
Py_ssize_t str1_len, str2_len;
1161-
const char *str1_data = PyUnicode_AsUTF8AndSize(str1, &str1_len);
1162-
const char *str2_data = PyUnicode_AsUTF8AndSize(str2, &str2_len);
1160+
const char *str1_data = _PyUnicode_AsUTF8NoNUL(str1);
1161+
const char *str2_data = _PyUnicode_AsUTF8NoNUL(str2);
11631162
if (str1_data == NULL || str2_data == NULL) {
11641163
return NULL;
11651164
}
1165+
Py_ssize_t str1_len = strlen(str1_data);
1166+
Py_ssize_t str2_len = strlen(str2_data);
1167+
11661168
/* Make sure sep and the NULL byte won't cause an overflow. */
11671169
assert(SIZE_MAX - str1_len - str2_len > 2);
11681170
size_t size = str1_len + 1 + str2_len + 1;

0 commit comments

Comments
 (0)