]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-127165: Disallow embedded NULL characters in `_interpreters` (#127199)
authorPeter Bierma <zintensitydev@gmail.com>
Sun, 1 Dec 2024 06:33:23 +0000 (01:33 -0500)
committerGitHub <noreply@github.com>
Sun, 1 Dec 2024 06:33:23 +0000 (06:33 +0000)
Lib/test/test_interpreters/test_api.py
Python/crossinterp.c

index a9befbba64daa049b6aae7160cb743da0240a854..01856d9bf67657418c6bca960f05f302c5d6595f 100644 (file)
@@ -1649,6 +1649,10 @@ class LowLevelTests(TestBase):
             self.assertIs(after2, None)
             self.assertEqual(after3.type.__name__, 'AssertionError')
 
+            with self.assertRaises(ValueError):
+                # GH-127165: Embedded NULL characters broke the lookup
+                _interpreters.set___main___attrs(interpid, {"\x00": 1})
+
         with self.subTest('from C-API'):
             with self.interpreter_from_capi() as interpid:
                 with self.assertRaisesRegex(InterpreterError, 'unrecognized'):
index 7aaa045f375cf0902adf655ad0e42f06e98018ac..0a106ad636bfe8e2b1e9df8607f99b91943c5b95 100644 (file)
@@ -342,6 +342,11 @@ _copy_string_obj_raw(PyObject *strobj, Py_ssize_t *p_size)
         return NULL;
     }
 
+    if (size != (Py_ssize_t)strlen(str)) {
+        PyErr_SetString(PyExc_ValueError, "found embedded NULL character");
+        return NULL;
+    }
+
     char *copied = PyMem_RawMalloc(size+1);
     if (copied == NULL) {
         PyErr_NoMemory();