Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -2071,6 +2071,10 @@ def test_new_config(self):
with self.subTest(f'bad override (gil={value!r})'):
with self.assertRaises(ValueError):
_interpreters.new_config(gil=value)
with self.subTest('bad override (gil=lone surrogate)'):
# gh-156117: a lone surrogate cannot be encoded as UTF-8
with self.assertRaises(UnicodeEncodeError):
_interpreters.new_config(gil='\ud800')

def test_get_main(self):
interpid, whence = _interpreters.get_main()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix a crash in :func:`!_interpreters.new_config`
when the ``gil`` argument cannot be encoded as UTF-8.
7 changes: 6 additions & 1 deletion Python/interpconfig.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,12 @@ _config_dict_copy_str(PyObject *dict, const char *name,
config_dict_invalid_type(name);
return -1;
}
strncpy(buf, PyUnicode_AsUTF8(item), bufsize-1);
const char *str = PyUnicode_AsUTF8(item);
if (str == NULL) {
Py_DECREF(item);
return -1;
}
strncpy(buf, str, bufsize-1);
buf[bufsize-1] = '\0';
Py_DECREF(item);
return 0;
Expand Down
Loading