From 870bb14bd0536dd29fc4a273f4884eb88e8331be Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Mon, 19 Oct 2026 12:34:30 -0700 Subject: [PATCH] gh-159739: Handle interpreter configuration encoding errors Propagate UTF-8 conversion errors before copying a string configuration value. Backport to 3.15 and 3.14. --- .../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;