Skip to content

Commit 9e262af

Browse files
gh-159739: Handle module filename encoding errors (#159749)
Propagate UTF-8 conversion errors before copying the module filename. Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
1 parent 5b0ab21 commit 9e262af

2 files changed

Lines changed: 5 additions & 0 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Avoid a crash when a module filename cannot be encoded as UTF-8.

‎Objects/moduleobject.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -992,6 +992,10 @@ _PyModule_GetFilenameUTF8(PyObject *mod, char *buffer, Py_ssize_t maxlen)
992992
}
993993
else {
994994
const char *filename = PyUnicode_AsUTF8AndSize(filenameobj, &size);
995+
if (filename == NULL) {
996+
Py_DECREF(filenameobj);
997+
return -1;
998+
}
995999
assert(size >= 0);
9961000
if (size > maxlen) {
9971001
size = -1;

0 commit comments

Comments
 (0)