From dd512d1e6160cd23fc06b6b5a961b7ba41a83f9d Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Mon, 19 Oct 2026 13:41:26 -0700 Subject: [PATCH 1/2] gh-159739: Release replaced PyInitConfig strings Backport to 3.15 and 3.14. --- .../C_API/2026-10-19-20-38-17.gh-issue-159739.eb9cdc.rst | 2 ++ Python/initconfig.c | 6 ++++-- 2 files changed, 6 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/C_API/2026-10-19-20-38-17.gh-issue-159739.eb9cdc.rst diff --git a/Misc/NEWS.d/next/C_API/2026-10-19-20-38-17.gh-issue-159739.eb9cdc.rst b/Misc/NEWS.d/next/C_API/2026-10-19-20-38-17.gh-issue-159739.eb9cdc.rst new file mode 100644 index 00000000000000..e3ca598d37fdce --- /dev/null +++ b/Misc/NEWS.d/next/C_API/2026-10-19-20-38-17.gh-issue-159739.eb9cdc.rst @@ -0,0 +1,2 @@ +:c:func:`PyInitConfig_SetStr` now releases a previously configured value and +preserves it if conversion of the replacement fails. diff --git a/Python/initconfig.c b/Python/initconfig.c index 363739a1a1b43c..9c31dbdf52e99f 100644 --- a/Python/initconfig.c +++ b/Python/initconfig.c @@ -4372,10 +4372,12 @@ PyInitConfig_SetStr(PyInitConfig *config, const char *name, const char* value) wchar_t **member = raw_member; - *member = utf8_to_wstr(config, value); - if (*member == NULL) { + wchar_t *wstr = utf8_to_wstr(config, value); + if (wstr == NULL) { return -1; } + free(*member); + *member = wstr; return 0; } From a1170c54a80e15fa5e5ba4d885055b25e6256adc Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Mon, 19 Oct 2026 14:21:20 -0700 Subject: [PATCH 2/2] Apply suggestion from @jelle-openai --- Python/initconfig.c | 1 + 1 file changed, 1 insertion(+) diff --git a/Python/initconfig.c b/Python/initconfig.c index 9c31dbdf52e99f..225ee0a3fe5ff2 100644 --- a/Python/initconfig.c +++ b/Python/initconfig.c @@ -4368,6 +4368,7 @@ PyInitConfig_SetStr(PyInitConfig *config, const char *name, const char* value) 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;