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
7 changes: 7 additions & 0 deletions Lib/test/test_interpreters/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,13 @@ def test_in_main(self):
# GH-126221: Passing an invalid Unicode character used to cause a SystemError
self.assertRaises(UnicodeEncodeError, _interpreters.create, '\udc80')

def test_config_with_surrogate_str_field(self):
# gh-148798: a config whose string field contains an unpaired
# surrogate used to crash the interpreter. It must raise instead.
config = _interpreters.new_config()
config.gil = 'own\udc80'
self.assertRaises(UnicodeEncodeError, _interpreters.create, config)

def test_in_thread(self):
lock = threading.Lock()
interp = None
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Fix a crash in :func:`!_interpreters.create` when a config value contains
a string with an unpaired surrogate. :c:func:`PyUnicode_AsUTF8` returned
``NULL`` and the result was passed to :c:func:`!strncpy`, dereferencing
it. The caller now propagates the :exc:`UnicodeEncodeError` instead.
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 *utf8 = PyUnicode_AsUTF8(item);
if (utf8 == NULL) {
Py_DECREF(item);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should maybe add a note to the just-raised exception to indicate for which key it failed. But this can be done as a follow-up.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will send as a follow-up PR.

return -1;
}
strncpy(buf, utf8, bufsize-1);
buf[bufsize-1] = '\0';
Py_DECREF(item);
return 0;
Expand Down
Loading