From 4ab61b94637f06529a294e03ac73cc19a93f2390 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Mon, 19 Oct 2026 19:30:32 -0700 Subject: [PATCH] gh-159739: Handle interpreter configuration encoding errors (GH-159747) Propagate UTF-8 conversion errors before copying a string configuration value. (cherry picked from commit 626394e0a38bdf7734567f00a09837f09e653bff) Co-authored-by: Jelle Zijlstra --- .../2026-10-19-19-33-35.gh-issue-159739.a8775b.rst | 2 ++ Python/interpconfig.c | 7 ++++++- 2 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2026-10-19-19-33-35.gh-issue-159739.a8775b.rst diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-19-19-33-35.gh-issue-159739.a8775b.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-19-19-33-35.gh-issue-159739.a8775b.rst new file mode 100644 index 00000000000000..73c2e9bd5d36b9 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-19-19-33-35.gh-issue-159739.a8775b.rst @@ -0,0 +1,2 @@ +Avoid a crash when an interpreter configuration string cannot be encoded as +UTF-8. diff --git a/Python/interpconfig.c b/Python/interpconfig.c index a37bd3f5b23a01..9ff28f88134318 100644 --- a/Python/interpconfig.c +++ b/Python/interpconfig.c @@ -133,7 +133,12 @@ _config_dict_copy_str(PyObject *dict, const char *name, config_dict_invalid_type(name); return -1; } - strncpy(buf, PyUnicode_AsUTF8(item), bufsize-1); + const char *str = PyUnicode_AsUTF8(item); + if (str == NULL) { + Py_DECREF(item); + return -1; + } + strncpy(buf, str, bufsize-1); buf[bufsize-1] = '\0'; Py_DECREF(item); return 0;