Skip to content

Commit

Permalink
pythongh-105375: Improve error handling in PyUnicode_BuildEncodingMap()
Browse files Browse the repository at this point in the history
  • Loading branch information
erlend-aasland committed Jun 8, 2023
1 parent 264a011 commit 17065c4
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions Objects/unicodeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -7934,25 +7934,29 @@ 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;
PyObject *key = PyLong_FromLong(PyUnicode_READ(kind, data, i));
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

0 comments on commit 17065c4

Please sign in to comment.