Skip to content

gh-123378: fix a crash in UnicodeError.__str__ #124935

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from 9 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
21 changes: 21 additions & 0 deletions Lib/test/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import weakref
import errno
from codecs import BOM_UTF8
from itertools import product
from textwrap import dedent

from test.support import (captured_stderr, check_impl_detail,
Expand Down Expand Up @@ -1336,6 +1337,26 @@ def test_unicode_errors_no_object(self):
for klass in klasses:
self.assertEqual(str(klass.__new__(klass)), "")

def test_unicode_error_str_gh_123378(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The purpose of the test is unclear to me, it just calls str(). Please add a comment to explain that you check that str() doesn't crash with a reference to the issue.

for formatter, start, end, obj in product(
(str, repr),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Testing repr() if we already test str() sounds redundant to me. Is it really worth it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No you're right. I'll remove it.

range(-5, 5),
range(-5, 5),
('', 'a', '123', '1234', '12345', 'abc123'),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
('', 'a', '123', '1234', '12345', 'abc123'),
('', 'a', 'abc', 'abcde', 'abcdef'),

):
with self.subTest(formatter, obj=obj, start=start, end=end):
exc = UnicodeEncodeError('utf-8', obj, start, end, '')
self.assertIsInstance(formatter(exc), str)

with self.subTest(formatter, obj=obj, start=start, end=end):
exc = UnicodeTranslateError(obj, start, end, '')
self.assertIsInstance(formatter(exc), str)

encoded = obj.encode()
with self.subTest(formatter, obj=encoded, start=start, end=end):
exc = UnicodeDecodeError('utf-8', encoded, start, end, '')
self.assertIsInstance(formatter(exc), str)

@no_tracing
def test_badisinstance(self):
# Bug #2542: if issubclass(e, MyException) raises an exception,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix a crash in the :meth:`~object.__str__` method of :exc:`UnicodeError`
objects when the :attr:`UnicodeError.start` and :attr:`UnicodeError.end`
values are invalid or out-of-range. Patch by Bénédikt Tran.
72 changes: 39 additions & 33 deletions Objects/exceptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -2994,26 +2994,29 @@ UnicodeEncodeError_init(PyObject *self, PyObject *args, PyObject *kwds)
static PyObject *
UnicodeEncodeError_str(PyObject *self)
{
PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self;
PyUnicodeErrorObject *exc = (PyUnicodeErrorObject *)self;
PyObject *result = NULL;
PyObject *reason_str = NULL;
PyObject *encoding_str = NULL;

if (!uself->object)
if (exc->object == NULL)
/* Not properly initialized. */
return PyUnicode_FromString("");

/* Get reason and encoding as strings, which they might not be if
they've been modified after we were constructed. */
reason_str = PyObject_Str(uself->reason);
reason_str = PyObject_Str(exc->reason);
if (reason_str == NULL)
goto done;
encoding_str = PyObject_Str(uself->encoding);
encoding_str = PyObject_Str(exc->encoding);
if (encoding_str == NULL)
goto done;

if (uself->start < PyUnicode_GET_LENGTH(uself->object) && uself->end == uself->start+1) {
Py_UCS4 badchar = PyUnicode_ReadChar(uself->object, uself->start);
Py_ssize_t len = PyUnicode_GET_LENGTH(exc->object);
Py_ssize_t start = exc->start, end = exc->end;

if ((start >= 0 && start < len) && (end >= 0 && end <= len) && end == start + 1) {
Py_UCS4 badchar = PyUnicode_ReadChar(exc->object, start);
const char *fmt;
if (badchar <= 0xff)
fmt = "'%U' codec can't encode character '\\x%02x' in position %zd: %U";
Expand All @@ -3025,15 +3028,15 @@ UnicodeEncodeError_str(PyObject *self)
fmt,
encoding_str,
(int)badchar,
uself->start,
start,
reason_str);
}
else {
result = PyUnicode_FromFormat(
"'%U' codec can't encode characters in position %zd-%zd: %U",
encoding_str,
uself->start,
uself->end-1,
start,
end - 1,
reason_str);
}
done:
Expand Down Expand Up @@ -3107,41 +3110,43 @@ UnicodeDecodeError_init(PyObject *self, PyObject *args, PyObject *kwds)
static PyObject *
UnicodeDecodeError_str(PyObject *self)
{
PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self;
PyUnicodeErrorObject *exc = (PyUnicodeErrorObject *)self;
PyObject *result = NULL;
PyObject *reason_str = NULL;
PyObject *encoding_str = NULL;

if (!uself->object)
if (exc->object == NULL)
/* Not properly initialized. */
return PyUnicode_FromString("");

/* Get reason and encoding as strings, which they might not be if
they've been modified after we were constructed. */
reason_str = PyObject_Str(uself->reason);
reason_str = PyObject_Str(exc->reason);
if (reason_str == NULL)
goto done;
encoding_str = PyObject_Str(uself->encoding);
encoding_str = PyObject_Str(exc->encoding);
if (encoding_str == NULL)
goto done;

if (uself->start < PyBytes_GET_SIZE(uself->object) && uself->end == uself->start+1) {
int byte = (int)(PyBytes_AS_STRING(((PyUnicodeErrorObject *)self)->object)[uself->start]&0xff);
Py_ssize_t len = PyBytes_GET_SIZE(exc->object);
Py_ssize_t start = exc->start, end = exc->end;

if ((start >= 0 && start < len) && (end >= 0 && end <= len) && end == start + 1) {
int badbyte = (int)(PyBytes_AS_STRING(exc->object)[start] & 0xff);
result = PyUnicode_FromFormat(
"'%U' codec can't decode byte 0x%02x in position %zd: %U",
encoding_str,
byte,
uself->start,
badbyte,
start,
reason_str);
}
else {
result = PyUnicode_FromFormat(
"'%U' codec can't decode bytes in position %zd-%zd: %U",
encoding_str,
uself->start,
uself->end-1,
reason_str
);
start,
end - 1,
reason_str);
}
done:
Py_XDECREF(reason_str);
Expand Down Expand Up @@ -3204,22 +3209,25 @@ UnicodeTranslateError_init(PyUnicodeErrorObject *self, PyObject *args,
static PyObject *
UnicodeTranslateError_str(PyObject *self)
{
PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self;
PyUnicodeErrorObject *exc = (PyUnicodeErrorObject *)self;
PyObject *result = NULL;
PyObject *reason_str = NULL;

if (!uself->object)
if (exc->object == NULL)
/* Not properly initialized. */
return PyUnicode_FromString("");

/* Get reason as a string, which it might not be if it's been
modified after we were constructed. */
reason_str = PyObject_Str(uself->reason);
reason_str = PyObject_Str(exc->reason);
if (reason_str == NULL)
goto done;

if (uself->start < PyUnicode_GET_LENGTH(uself->object) && uself->end == uself->start+1) {
Py_UCS4 badchar = PyUnicode_ReadChar(uself->object, uself->start);
Py_ssize_t len = PyUnicode_GET_LENGTH(exc->object);
Py_ssize_t start = exc->start, end = exc->end;

if ((start >= 0 && start < len) && (end >= 0 && end <= len) && end == start + 1) {
Py_UCS4 badchar = PyUnicode_ReadChar(exc->object, start);
const char *fmt;
if (badchar <= 0xff)
fmt = "can't translate character '\\x%02x' in position %zd: %U";
Expand All @@ -3230,16 +3238,14 @@ UnicodeTranslateError_str(PyObject *self)
result = PyUnicode_FromFormat(
fmt,
(int)badchar,
uself->start,
reason_str
);
start,
reason_str);
} else {
result = PyUnicode_FromFormat(
"can't translate characters in position %zd-%zd: %U",
uself->start,
uself->end-1,
reason_str
);
start,
end - 1,
reason_str);
}
done:
Py_XDECREF(reason_str);
Expand Down
Loading