mirror of
https://github.com/python/cpython.git
synced 2025-07-25 12:14:38 +00:00
[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 555be81026
)
Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
This commit is contained in:
parent
05c73e1cd8
commit
91877478ed
2 changed files with 19 additions and 12 deletions
|
@ -0,0 +1,2 @@
|
||||||
|
Improve error handling in :c:func:`PyUnicode_BuildEncodingMap` where an
|
||||||
|
exception could end up being overwritten.
|
|
@ -8454,25 +8454,30 @@ PyUnicode_BuildEncodingMap(PyObject* string)
|
||||||
|
|
||||||
if (need_dict) {
|
if (need_dict) {
|
||||||
PyObject *result = PyDict_New();
|
PyObject *result = PyDict_New();
|
||||||
PyObject *key, *value;
|
|
||||||
if (!result)
|
if (!result)
|
||||||
return NULL;
|
return NULL;
|
||||||
for (i = 0; i < length; i++) {
|
for (i = 0; i < length; i++) {
|
||||||
key = PyLong_FromLong(PyUnicode_READ(kind, data, i));
|
Py_UCS4 c = PyUnicode_READ(kind, data, i);
|
||||||
value = PyLong_FromLong(i);
|
PyObject *key = PyLong_FromLong(c);
|
||||||
if (!key || !value)
|
if (key == NULL) {
|
||||||
goto failed1;
|
Py_DECREF(result);
|
||||||
if (PyDict_SetItem(result, key, value) == -1)
|
return NULL;
|
||||||
goto failed1;
|
}
|
||||||
|
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(key);
|
||||||
Py_DECREF(value);
|
Py_DECREF(value);
|
||||||
|
if (rc < 0) {
|
||||||
|
Py_DECREF(result);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
failed1:
|
|
||||||
Py_XDECREF(key);
|
|
||||||
Py_XDECREF(value);
|
|
||||||
Py_DECREF(result);
|
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Create a three-level trie */
|
/* Create a three-level trie */
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue