summaryrefslogtreecommitdiff
path: root/Objects/unicodeobject.c
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@redhat.com>2018-11-28 10:26:20 +0100
committerGitHub <noreply@github.com>2018-11-28 10:26:20 +0100
commitbde9d6bbb46ca59bcee5d5060adaa33c3ffee3a6 (patch)
treeef525570f785225ab3da54b867b2ea4cb137fb40 /Objects/unicodeobject.c
parenta22df4896f6b83c8741203118790ae281716bca5 (diff)
downloadcpython-git-bde9d6bbb46ca59bcee5d5060adaa33c3ffee3a6.tar.gz
bpo-34523, bpo-35322: Fix unicode_encode_locale() (GH-10759)
Fix memory leak in PyUnicode_EncodeLocale() and PyUnicode_EncodeFSDefault() on error handling. Changes: * Fix unicode_encode_locale() error handling * Fix test_codecs.LocaleCodecTest
Diffstat (limited to 'Objects/unicodeobject.c')
-rw-r--r--Objects/unicodeobject.c12
1 files changed, 5 insertions, 7 deletions
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
index 2b1db918a1..bc98c44c74 100644
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -3449,10 +3449,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;
}
@@ -3461,6 +3460,8 @@ unicode_encode_locale(PyObject *unicode, const char *errors,
const char *reason;
int res = _Py_EncodeLocaleEx(wstr, &str, &error_pos, &reason,
current_locale, error_handler);
+ PyMem_Free(wstr);
+
if (res != 0) {
if (res == -2) {
PyObject *exc;
@@ -3473,18 +3474,15 @@ unicode_encode_locale(PyObject *unicode, const char *errors,
PyCodec_StrictErrors(exc);
Py_DECREF(exc);
}
- return NULL;
}
else if (res == -3) {
PyErr_SetString(PyExc_ValueError, "unsupported error handler");
}
else {
PyErr_NoMemory();
- PyMem_Free(wstr);
- return NULL;
}
+ return NULL;
}
- PyMem_Free(wstr);
PyObject *bytes = PyBytes_FromString(str);
PyMem_RawFree(str);