Skip to content

Commit

Permalink
bpo-34523, bpo-35322: Fix unicode_encode_locale() (GH-10759) (GH-10761)
Browse files Browse the repository at this point in the history
Fix memory leak in PyUnicode_EncodeLocale() and
PyUnicode_EncodeFSDefault() on error handling.

Fix unicode_encode_locale() error handling.

(cherry picked from commit bde9d6b)
  • Loading branch information
vstinner committed Nov 28, 2018
1 parent 80db40c commit 85ab974
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
@@ -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
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

0 comments on commit 85ab974

Please sign in to comment.