8000 gh-123378: fix a crash in `UnicodeError.__str__` by picnixz · Pull Request #124935 · python/cpython · GitHub
[go: up one dir, main page]

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
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix UnicodeTranslateError
  • Loading branch information
picnixz committed Oct 4, 2024
commit 2313cd1def7843e9d5f42690805c7ca1373d8b8f
55 changes: 28 additions & 27 deletions Objects/exceptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -3205,46 +3205,47 @@ UnicodeTranslateError_init(PyUnicodeErrorObject *self, PyObject *args,
static PyObject *
UnicodeTranslateError_str(PyObject *self)
{
PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self;
PyObject *result = NULL;
PyObject *reason_str = NULL;
PyUnicodeErrorObject *exc = (PyUnicodeErrorObject *)self;

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);
if (reason_str == NULL)
goto done;
PyObject *reason = PyObject_Str(exc->reason);
if (reason == NULL) {
return NULL;
}

PyObject *res;
Py_ssize_t len = PyUnicode_GET_LENGTH(exc->object);
Py_ssize_t start = exc->start, end = exc->end;

if (uself->start < PyUnicode_GET_LENGTH(uself->object) && uself->end == uself->start+1) {
Py_UCS4 badchar = PyUnicode_ReadChar(uself->object, uself->start);
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)
if (badchar <= 0xff) {
fmt = "can't translate character '\\x%02x' in position %zd: %U";
else if (badchar <= 0xffff)
}
else if (badchar <= 0xffff) {
fmt = "can't translate character '\\u%04x' in position %zd: %U";
else
}
else {
fmt = "can't translate character '\\U%08x' in position %zd: %U";
result = PyUnicode_FromFormat(
fmt,
(int)badchar,
uself->start,
reason_str
);
} else {
result = PyUnicode_FromFormat(
}
res = PyUnicode_FromFormat(fmt, (int)badchar, start, reason);
}
else {
res = PyUnicode_FromFormat(
"can't translate characters in position %zd-%zd: %U",
uself->start,
uself->end-1,
reason_str
);
start, end - 1, reason
);
}
done:
Py_XDECREF(reason_str);
return result;

Py_DECREF(reason);
return res;
}

static PyTypeObject _PyExc_UnicodeTranslateError = {
Expand Down
0