Skip to content
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

[3.12] gh-105375: Improve error handling in PyUnicode_BuildEncodingMap() (GH-105491) #105661

Merged
merged 1 commit into from
Jun 11, 2023
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Improve error handling in :c:func:`PyUnicode_BuildEncodingMap` where an
exception could end up being overwritten.
29 changes: 17 additions & 12 deletions Objects/unicodeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -7934,25 +7934,30 @@ PyUnicode_BuildEncodingMap(PyObject* string)

if (need_dict) {
PyObject *result = PyDict_New();
PyObject *key, *value;
if (!result)
return NULL;
for (i = 0; i < length; i++) {
key = PyLong_FromLong(PyUnicode_READ(kind, data, i));
value = PyLong_FromLong(i);
if (!key || !value)
goto failed1;
if (PyDict_SetItem(result, key, value) == -1)
goto failed1;
Py_UCS4 c = PyUnicode_READ(kind, data, i);
PyObject *key = PyLong_FromLong(c);
if (key == NULL) {
Py_DECREF(result);
return NULL;
}
PyObject *value = PyLong_FromLong(i);
if (value == NULL) {
Py_DECREF(key);
Py_DECREF(result);
return NULL;
}
int rc = PyDict_SetItem(result, key, value);
Py_DECREF(key);
Py_DECREF(value);
if (rc < 0) {
Py_DECREF(result);
return NULL;
}
}
return result;
failed1:
Py_XDECREF(key);
Py_XDECREF(value);
Py_DECREF(result);
return NULL;
}

/* Create a three-level trie */
Expand Down