Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions 14 Lib/test/test_codecs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3290,9 +3290,9 @@ def check_encode_strings(self, errors):
expected = text.encode(self.ENCODING, errors)
except UnicodeEncodeError:
with self.assertRaises(RuntimeError) as cm:
self.encode(self.SURROGATES)
self.encode(text, errors)
errmsg = str(cm.exception)
self.assertTrue(errmsg.startswith("encode error: pos=0, reason="), errmsg)
self.assertRegex(errmsg, r"encode error: pos=[0-9]+, reason=")
else:
encoded = self.encode(text, errors)
self.assertEqual(encoded, expected)
Expand All @@ -3315,6 +3315,11 @@ def test_encode_surrogatepass(self):

self.check_encode_strings("surrogatepass")

def test_encode_unsupported_error_handler(self):
with self.assertRaises(ValueError) as cm:
self.encode('', 'backslashreplace')
self.assertEqual(str(cm.exception), 'unsupported error handler')

def decode(self, encoded, errors="strict"):
return _testcapi.DecodeLocaleEx(encoded, 0, errors)

Expand Down Expand Up @@ -3370,6 +3375,11 @@ def test_decode_surrogatepass(self):

self.check_decode_strings("surrogatepass")

def test_decode_unsupported_error_handler(self):
with self.assertRaises(ValueError) as cm:
self.decode(b'', 'backslashreplace')
self.assertEqual(str(cm.exception), 'unsupported error handler')


if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -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 12 Objects/unicodeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
Expand All @@ -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);
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.