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:
parent
80db40cdd6
commit
85ab974f78
|
@ -0,0 +1,2 @@
|
|||
Fix memory leak in :c:func:`PyUnicode_EncodeLocale` and
|
||||
:c:func:`PyUnicode_EncodeFSDefault` on error handling.
|
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue