Repository navigation
Closed
Labels
interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)topic-C-APItype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Description
Bug report
Potentially, if "value" equals NULL and "spec->type" equals "PyConfig_MEMBER_WSTR_OPT", null will be passed into "utf8_to_wstr" function and might cause a segmentation fault in strlen
Lines 4369 to 4376 in d3a1e72
| if (value == NULL && spec->type != PyConfig_MEMBER_WSTR_OPT) { | |
| initconfig_set_error(config, "config option string cannot be NULL"); | |
| return -1; | |
| } | |
| wchar_t **member = raw_member; | |
| wchar_t *wstr = utf8_to_wstr(config, value); |
Lines 4324 to 4328 in d3a1e72
| utf8_to_wstr(PyInitConfig *config, const char *str) | |
| { | |
| wchar_t *wstr; | |
| size_t wlen; | |
| int res = _Py_DecodeUTF8(str, strlen(str), &wstr, &wlen, _Py_ERROR_STRICT); |
Documentation states that "value" is a null-terminated UTF-8 string, but isn't clearly stating that Null is allowed
Suggested fix: separate "value" and "spec->type" checks
if (value == NULL) {
if (spec->type != PyConfig_MEMBER_WSTR_OPT) {
nitconfig_set_error(config, "config option string cannot be NULL");
return -1;
}
wchar_t **member = raw_member;
free(*member);
*member = wstr;
return 0;
}
CPython versions tested on:
CPython main branch
Operating systems tested on:
No response
Linked PRs
Metadata
Metadata
Assignees
Labels
interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)topic-C-APItype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Activity
[-]Null derefication of a pointer in initconfig.c[/-][+]Null dereferencing of a pointer in initconfig.c[/+]lazerg commentedon Oct 30, 2026
I will work on it if maintainers approve
picnixz commentedon Oct 30, 2026
How can a NUL-terminated UTF8 string be NULL? are other functions checking for nullity? i believe this is unnecessary churn.
Aruise commentedon Oct 30, 2026
@picnixz
It does describe "value" as a null-terminated UTF-8 string, yes, which indeed does not explicitly allow NULL
However, the implementation explicitly checks whether "value" is NULL, suggesting that NULL might have been intended to be supported at some point
NekoAsakura commentedon Oct 30, 2026
Reproduced on
main:Original implementation (#123502) added the NULL check with an explicit exemption for
PyConfig_MEMBER_WSTR_OPT, but had noreturn -1:cpython/Python/initconfig.c
Lines 3809 to 3815 in ef9d547
It was partially fixed in #159769 (comment), but left the
WSTR_OPTexemption as-is.cc @vstinner
sobolevn commentedon Nov 3, 2026
This was fixed in 395d766
NekoAsakura commentedon Nov 3, 2026
That only fixed the non-
WSTR_OPTpath.pythongh-160962: Fix PyInitConfig_SetStr() with NULL value
vstinner commentedon Nov 3, 2026
Hi. I wrote #161258 to fix PyInitConfig_SetStr() crash when the value is NULL. The PR also adds multiple tests on all Get functions.
gh-160962: Fix PyInitConfig_SetStr() with NULL value (#161258)
[3.15] gh-160962: Fix PyInitConfig_SetStr() with NULL value (GH-161258)…
[3.14] gh-160962: Fix PyInitConfig_SetStr() with NULL value (GH-161258)…
vstinner commentedon Nov 4, 2026
Thanks for the bug report, I merged my fix.