Skip to content

Commit 85ab974

Browse files
authored
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 bde9d6b)
1 parent 80db40c commit 85ab974

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix memory leak in :c:func:`PyUnicode_EncodeLocale` and
2+
:c:func:`PyUnicode_EncodeFSDefault` on error handling.

Objects/unicodeobject.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3391,10 +3391,9 @@ unicode_encode_locale(PyObject *unicode, const char *errors,
33913391
return NULL;
33923392
}
33933393

3394-
Py_ssize_t wlen2 = wcslen(wstr);
3395-
if (wlen2 != wlen) {
3396-
PyMem_Free(wstr);
3394+
if ((size_t)wlen != wcslen(wstr)) {
33973395
PyErr_SetString(PyExc_ValueError, "embedded null character");
3396+
PyMem_Free(wstr);
33983397
return NULL;
33993398
}
34003399

@@ -3403,6 +3402,8 @@ unicode_encode_locale(PyObject *unicode, const char *errors,
34033402
const char *reason;
34043403
int res = _Py_EncodeLocaleEx(wstr, &str, &error_pos, &reason,
34053404
current_locale, surrogateescape);
3405+
PyMem_Free(wstr);
3406+
34063407
if (res != 0) {
34073408
if (res == -2) {
34083409
PyObject *exc;
@@ -3415,15 +3416,12 @@ unicode_encode_locale(PyObject *unicode, const char *errors,
34153416
PyCodec_StrictErrors(exc);
34163417
Py_DECREF(exc);
34173418
}
3418-
return NULL;
34193419
}
34203420
else {
34213421
PyErr_NoMemory();
3422-
PyMem_Free(wstr);
3423-
return NULL;
34243422
}
3423+
return NULL;
34253424
}
3426-
PyMem_Free(wstr);
34273425

34283426
PyObject *bytes = PyBytes_FromString(str);
34293427
PyMem_RawFree(str);

0 commit comments

Comments
 (0)