From 2b86eb16bfabc534caf0189ebf3ee456a67468f3 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Mon, 19 Oct 2026 19:30:21 -0700 Subject: [PATCH] gh-159739: Handle module filename encoding errors (GH-159749) Propagate UTF-8 conversion errors before copying the module filename. (cherry picked from commit 9e262af9aebbad2abc975649036d6fc455bcb194) Co-authored-by: Jelle Zijlstra Co-authored-by: Jelle Zijlstra --- .../2026-10-19-19-33-35.gh-issue-159739.09590b.rst | 1 + Objects/moduleobject.c | 4 ++++ 2 files changed, 5 insertions(+) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2026-10-19-19-33-35.gh-issue-159739.09590b.rst diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-19-19-33-35.gh-issue-159739.09590b.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-19-19-33-35.gh-issue-159739.09590b.rst new file mode 100644 index 000000000000000..3231be417aa80ec --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-19-19-33-35.gh-issue-159739.09590b.rst @@ -0,0 +1 @@ +Avoid a crash when a module filename cannot be encoded as UTF-8. diff --git a/Objects/moduleobject.c b/Objects/moduleobject.c index 7a5d14c538391dc..7c842f3ed8b1965 100644 --- a/Objects/moduleobject.c +++ b/Objects/moduleobject.c @@ -992,6 +992,10 @@ _PyModule_GetFilenameUTF8(PyObject *mod, char *buffer, Py_ssize_t maxlen) } else { const char *filename = PyUnicode_AsUTF8AndSize(filenameobj, &size); + if (filename == NULL) { + Py_DECREF(filenameobj); + return -1; + } assert(size >= 0); if (size > maxlen) { size = -1;