Skip to content

Commit

Permalink
pythongh-127165: Disallow embedded NULL characters in _interpreters (
Browse files Browse the repository at this point in the history
  • Loading branch information
ZeroIntensity authored and picnixz committed Dec 2, 2024
1 parent 9a244eb commit 7321eff
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Lib/test/test_interpreters/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1649,6 +1649,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'):
Expand Down
5 changes: 5 additions & 0 deletions Python/crossinterp.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 7321eff

Please sign in to comment.