https://github.com/python/cpython/commit/2813a6167560a8d649465249f27ff1c134151387
commit: 2813a6167560a8d649465249f27ff1c134151387
branch: 3.13
author: Miss Islington (bot) <[email protected]>
committer: kumaraditya303 <[email protected]>
date: 2024-12-01T06:55:55Z
summary:

[3.13] gh-127165: Disallow embedded NULL characters in `_interpreters` 
(GH-127199) (#127463)

gh-127165: Disallow embedded NULL characters in `_interpreters` (GH-127199)
(cherry picked from commit 46bfd26fb294a8769ef6d0c056ee6c9df022a037)

Co-authored-by: Peter Bierma <[email protected]>

files:
M Lib/test/test_interpreters/test_api.py
M Python/crossinterp.c

diff --git a/Lib/test/test_interpreters/test_api.py 
b/Lib/test/test_interpreters/test_api.py
index 42819ae9b180b4..61beb317c37975 100644
--- a/Lib/test/test_interpreters/test_api.py
+++ b/Lib/test/test_interpreters/test_api.py
@@ -1650,6 +1650,10 @@ def test_set___main___attrs(self):
             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'):
diff --git a/Python/crossinterp.c b/Python/crossinterp.c
index 01bb45d23bac1b..ac379a1ad0fb6d 100644
--- a/Python/crossinterp.c
+++ b/Python/crossinterp.c
@@ -346,6 +346,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();

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to