bpo-34523, bpo-35322: Fix unicode_encode_locale() (GH-10759) (GH-10761)

Fix memory leak in PyUnicode_EncodeLocale() and
PyUnicode_EncodeFSDefault() on error handling.

Fix unicode_encode_locale() error handling.

(cherry picked from commit bde9d6bbb4)
This commit is contained in:
Victor Stinner 2018-11-28 12:42:40 +01:00 committed by GitHub
parent 80db40cdd6
commit 85ab974f78
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 7 deletions

View File

@ -0,0 +1,2 @@
Fix memory leak in :c:func:`PyUnicode_EncodeLocale` and
:c:func:`PyUnicode_EncodeFSDefault` on error handling.

View File

@ -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;
}
@ -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;
@ -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);