[Python-checkins] bpo-34523, bpo-35322: Fix unicode_encode_locale() (GH-10759) (GH-10761)
Victor Stinner
webhook-mailer at python.org
Wed Nov 28 06:42:44 EST 2018
https://github.com/python/cpython/commit/85ab974f78c0ebcfa611639864640d0273eb5466
commit: 85ab974f78c0ebcfa611639864640d0273eb5466
branch: 3.7
author: Victor Stinner <vstinner at redhat.com>
committer: GitHub <noreply at github.com>
date: 2018-11-28T12:42:40+01:00
summary:
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 bde9d6bbb46ca59bcee5d5060adaa33c3ffee3a6)
files:
A Misc/NEWS.d/next/C API/2018-11-28-03-20-36.bpo-35322.Qcqsag.rst
M Objects/unicodeobject.c
diff --git a/Misc/NEWS.d/next/C API/2018-11-28-03-20-36.bpo-35322.Qcqsag.rst b/Misc/NEWS.d/next/C API/2018-11-28-03-20-36.bpo-35322.Qcqsag.rst
new file mode 100644
index 000000000000..f5b4796307f7
--- /dev/null
+++ b/Misc/NEWS.d/next/C API/2018-11-28-03-20-36.bpo-35322.Qcqsag.rst
@@ -0,0 +1,2 @@
+Fix memory leak in :c:func:`PyUnicode_EncodeLocale` and
+:c:func:`PyUnicode_EncodeFSDefault` on error handling.
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
index e6371d2337c3..bd3f151c6a50 100644
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -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);
More information about the Python-checkins
mailing list