From: Victor Stinner Date: Wed, 28 Nov 2018 11:42:40 +0000 (+0100) Subject: bpo-34523, bpo-35322: Fix unicode_encode_locale() (GH-10759) (GH-10761) X-Git-Tag: v3.7.2rc1~90 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=85ab974f78c0ebcfa611639864640d0273eb5466;p=thirdparty%2FPython%2Fcpython.git 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) --- 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);