Fix memory leak in PyUnicode_EncodeLocale() and
PyUnicode_EncodeFSDefault() on error handling.
Fix unicode_encode_locale() error handling.
(cherry picked from commit
bde9d6bbb46ca59bcee5d5060adaa33c3ffee3a6)
--- /dev/null
+Fix memory leak in :c:func:`PyUnicode_EncodeLocale` and
+:c:func:`PyUnicode_EncodeFSDefault` on error handling.
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;
}
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;
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);