Skip to content

Commit 395d766

Browse files
gh-159739: Release replaced PyInitConfig strings (GH-159769)
* gh-159739: Release replaced PyInitConfig strings Backport to 3.15 and 3.14. --------- Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
1 parent da6629c commit 395d766

2 files changed

Lines changed: 7 additions & 2 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
:c:func:`PyInitConfig_SetStr` now releases a previously configured value and
2+
preserves it if conversion of the replacement fails.

‎Python/initconfig.c‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4368,14 +4368,17 @@ PyInitConfig_SetStr(PyInitConfig *config, const char *name, const char* value)
43684368

43694369
if (value == NULL && spec->type != PyConfig_MEMBER_WSTR_OPT) {
43704370
initconfig_set_error(config, "config option string cannot be NULL");
4371+
return -1;
43714372
}
43724373

43734374
wchar_t **member = raw_member;
43744375

4375-
*member = utf8_to_wstr(config, value);
4376-
if (*member == NULL) {
4376+
wchar_t *wstr = utf8_to_wstr(config, value);
4377+
if (wstr == NULL) {
43774378
return -1;
43784379
}
4380+
free(*member);
4381+
*member = wstr;
43794382
return 0;
43804383
}
43814384

0 commit comments

Comments
 (0)