Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix memory leak in :c:func:`PyUnicode_EncodeLocale` and
:c:func:`PyUnicode_EncodeFSDefault` on error handling.
12 changes: 5 additions & 7 deletions Objects/unicodeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -3391,10 +3391,9 @@ unicode_encode_locale(PyObject *unicode, const char *errors,
return NULL;
}

Py_ssize_t wlen2 = wcslen(wstr);
if (wlen2 != wlen) {
PyMem_Free(wstr);
if ((size_t)wlen != wcslen(wstr)) {
PyErr_SetString(PyExc_ValueError, "embedded null character");
PyMem_Free(wstr);
return NULL;
}

Expand All @@ -3403,6 +3402,8 @@ unicode_encode_locale(PyObject *unicode, const char *errors,
const char *reason;
int res = _Py_EncodeLocaleEx(wstr, &str, &error_pos, &reason,
current_locale, surrogateescape);
PyMem_Free(wstr);

if (res != 0) {
if (res == -2) {
PyObject *exc;
Expand All @@ -3415,15 +3416,12 @@ unicode_encode_locale(PyObject *unicode, const char *errors,
PyCodec_StrictErrors(exc);
Py_DECREF(exc);
}
return NULL;
}
else {
PyErr_NoMemory();
PyMem_Free(wstr);
return NULL;
}
return NULL;
}
PyMem_Free(wstr);

PyObject *bytes = PyBytes_FromString(str);
PyMem_RawFree(str);
Expand Down