]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-127208: Reject null character in _imp.create_dynamic() (#127400)
authorVictor Stinner <vstinner@python.org>
Fri, 29 Nov 2024 15:20:38 +0000 (16:20 +0100)
committerGitHub <noreply@github.com>
Fri, 29 Nov 2024 15:20:38 +0000 (16:20 +0100)
_imp.create_dynamic() now rejects embedded null characters in the
path and in the module name.

Lib/test/test_import/__init__.py
Python/import.c

index 0e39998ebb3783e171b8cc51982997fae6255ea1..c52b7f3e09bea1590b82f3d3896dfd39e78c3f25 100644 (file)
@@ -1133,6 +1133,19 @@ except ImportError as e:
                 stdout, stderr = popen.communicate()
                 self.assertRegex(stdout, expected_error)
 
+    def test_create_dynamic_null(self):
+        with self.assertRaisesRegex(ValueError, 'embedded null character'):
+            class Spec:
+                name = "a\x00b"
+                origin = "abc"
+            _imp.create_dynamic(Spec())
+
+        with self.assertRaisesRegex(ValueError, 'embedded null character'):
+            class Spec2:
+                name = "abc"
+                origin = "a\x00b"
+            _imp.create_dynamic(Spec2())
+
 
 @skip_if_dont_write_bytecode
 class FilePermissionTests(unittest.TestCase):
index 09fe95fa1fb64783fa55640a468412e8b8da55dd..b3c384c27718ce6217d00a89804ce330739b7536 100644 (file)
@@ -1157,12 +1157,14 @@ del_extensions_cache_value(struct extensions_cache_value *value)
 static void *
 hashtable_key_from_2_strings(PyObject *str1, PyObject *str2, const char sep)
 {
-    Py_ssize_t str1_len, str2_len;
-    const char *str1_data = PyUnicode_AsUTF8AndSize(str1, &str1_len);
-    const char *str2_data = PyUnicode_AsUTF8AndSize(str2, &str2_len);
+    const char *str1_data = _PyUnicode_AsUTF8NoNUL(str1);
+    const char *str2_data = _PyUnicode_AsUTF8NoNUL(str2);
     if (str1_data == NULL || str2_data == NULL) {
         return NULL;
     }
+    Py_ssize_t str1_len = strlen(str1_data);
+    Py_ssize_t str2_len = strlen(str2_data);
+
     /* Make sure sep and the NULL byte won't cause an overflow. */
     assert(SIZE_MAX - str1_len - str2_len > 2);
     size_t size = str1_len + 1 + str2_len + 1;