diff --git a/Misc/NEWS.d/next/Library/2026-06-20-15-18-46.gh-issue-151763.t-jCFF.rst b/Misc/NEWS.d/next/Library/2026-06-20-15-18-46.gh-issue-151763.t-jCFF.rst new file mode 100644 index 00000000000000..8e0ee35dfb412e --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-06-20-15-18-46.gh-issue-151763.t-jCFF.rst @@ -0,0 +1,4 @@ +Fix a possible NULL dereference in ``os.path.normpath()`` for bytes paths +when memory allocation fails. The function now correctly propagates +``MemoryError`` instead of passing a NULL object to +``PyUnicode_EncodeFSDefault()``. diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index 1f1b7fa729c01c..c52d2e7d5bfbd9 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -6145,6 +6145,9 @@ os__path_normpath_impl(PyObject *module, path_t *path) else { result = PyUnicode_FromWideChar(norm_path, norm_len); } + if (result == NULL) { + return NULL; + } if (PyBytes_Check(path->object)) { Py_SETREF(result, PyUnicode_EncodeFSDefault(result)); }