gh-148798: Fix crash in _interpreters.create() on non-UTF-8 string in config#148799
gh-148798: Fix crash in _interpreters.create() on non-UTF-8 string in config#148799rajkripal wants to merge 2 commits intopython:mainfrom
Conversation
…n config _config_dict_copy_str passed the result of PyUnicode_AsUTF8 straight to strncpy. When the string contained an unpaired surrogate, PyUnicode_AsUTF8 returned NULL and set UnicodeEncodeError, and strncpy dereferenced NULL. Check the return value and propagate the error, mirroring the pattern used at Modules/_interpretersmodule.c:425. Add a regression test alongside the existing pythongh-126221 case.
| @@ -0,0 +1,5 @@ | |||
| Fix a crash in :func:`!_interpreters.create` when a config object passes a | |||
| string with an unpaired surrogate as a value (for example ``gil``). The | |||
There was a problem hiding this comment.
This can be confused with gil being the "bad" word. Instead, just mention that the crash was fixed if the value had unpaires surrogates (but do not mention which keys they could be associated with)
| @@ -0,0 +1,5 @@ | |||
| Fix a crash in :func:`!_interpreters.create` when a config object passes a | |||
| string with an unpaired surrogate as a value (for example ``gil``). The | |||
| internal helper ``_config_dict_copy_str`` now checks the return of | |||
There was a problem hiding this comment.
I do not think it is necessary to mention the internals. Just mentioning interpreter's possible failures should be enough.
| # GH-126221: Passing an invalid Unicode character used to cause a SystemError | ||
| self.assertRaises(UnicodeEncodeError, _interpreters.create, '\udc80') | ||
|
|
||
| # A config object with a surrogate in a string field must raise, not crash. |
| self.assertRaises(UnicodeEncodeError, _interpreters.create, '\udc80') | ||
|
|
||
| # A config object with a surrogate in a string field must raise, not crash. | ||
| class BadConfig: |
There was a problem hiding this comment.
Is there a need to set all other fields?
There was a problem hiding this comment.
Switched to _interpreters.new_config() in 02053a9 so only .gil is set. Also fixes the CI failure on main where BadConfig()'s instance dict was empty.
| strncpy(buf, PyUnicode_AsUTF8(item), bufsize-1); | ||
| const char *utf8 = PyUnicode_AsUTF8(item); | ||
| if (utf8 == NULL) { | ||
| Py_DECREF(item); |
There was a problem hiding this comment.
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.
Reword the NEWS entry to drop the named config key and the reference to internal helpers. Move the regression test into its own method in CreateTests, and build the config from _interpreters.new_config() instead of a hand-rolled class, so only the surrogate path is exercised. Using new_config() also fixes the test on main, where an instance of BadConfig had an empty __dict__ and was rejected before the surrogate check.
_config_dict_copy_str(Python/interpconfig.c) passed the result ofPyUnicode_AsUTF8()straight tostrncpy(). When the string can't beUTF-8 encoded — e.g. it contains an unpaired surrogate —
PyUnicode_AsUTF8returns
NULLand setsUnicodeEncodeError, andstrncpythendereferences
NULL, segfaulting the interpreter.Lone surrogates are reachable from pure Python (
'\udc80',chr(0xDC80))and also show up via
surrogateescapewhen non-UTF-8 filenames / envvars / argv get forwarded into a config dict.
Fix: check the return of
PyUnicode_AsUTF8and propagate the error,mirroring the pattern already used at
Modules/_interpretersmodule.c:425.Regression test lives next to the existing gh-126221 case in
CreateTests.test_in_main.