gh-148798: Fix crash in _interpreters.create() on non-UTF-8 string in config#148799
Open
rajkripal wants to merge 1 commit intopython:mainfrom
Open
gh-148798: Fix crash in _interpreters.create() on non-UTF-8 string in config#148799rajkripal wants to merge 1 commit intopython:mainfrom
rajkripal wants to merge 1 commit 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.
picnixz
reviewed
Apr 20, 2026
| @@ -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 | |||
Member
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 | |||
Member
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: |
Member
There was a problem hiding this comment.
Is there a need to set all other fields?
| strncpy(buf, PyUnicode_AsUTF8(item), bufsize-1); | ||
| const char *utf8 = PyUnicode_AsUTF8(item); | ||
| if (utf8 == NULL) { | ||
| Py_DECREF(item); |
Member
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
_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.