diff options
| author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2023-06-11 20:01:52 (GMT) |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-06-11 20:01:52 (GMT) |
| commit | 91877478ed68fd56934c699e03f81da9f4242d3c (patch) | |
| tree | f6a50fe14abfee5c53ca9a937842e95b339087d7 /Objects/unicodeobject.c | |
| parent | 05c73e1cd872843952f0e769265ec3d9535488f6 (diff) | |
| download | cpython-91877478ed68fd56934c699e03f81da9f4242d3c.zip cpython-91877478ed68fd56934c699e03f81da9f4242d3c.tar.gz cpython-91877478ed68fd56934c699e03f81da9f4242d3c.tar.bz2 | |
[3.11] gh-105375: Improve error handling in PyUnicode_BuildEncodingMap() (GH-105491) (#105662)
Bail on first error to prevent exceptions from possibly being overwritten.
(cherry picked from commit 555be81026fe1205d16c02f6321221381174cd07)
Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
Diffstat (limited to 'Objects/unicodeobject.c')
| -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 4bdc8931..50deefe 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -8454,25 +8454,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 */ |
