diff options
author | Erlend E. Aasland <erlend.aasland@protonmail.com> | 2023-06-11 19:29:19 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-06-11 19:29:19 (GMT) |
commit | 555be81026fe1205d16c02f6321221381174cd07 (patch) | |
tree | 897b0d987cb50f43bf5cab9725e096b903cca9f6 /Objects | |
parent | 567d6ae8e77579173510fc948ac06b2ababf3d40 (diff) | |
download | cpython-555be81026fe1205d16c02f6321221381174cd07.zip cpython-555be81026fe1205d16c02f6321221381174cd07.tar.gz cpython-555be81026fe1205d16c02f6321221381174cd07.tar.bz2 |
gh-105375: Improve error handling in PyUnicode_BuildEncodingMap() (#105491)
Bail on first error to prevent exceptions from possibly being overwritten.
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/unicodeobject.c | 29 |
1 files changed, 17 insertions, 12 deletions
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index ffb4a87..38b72de 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -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 */ |