gh-105375: Improve error handling in PyUnicode_BuildEncodingMap() (#105491)

Bail on first error to prevent exceptions from possibly being overwritten.
This commit is contained in:
Erlend E. Aasland 2023-06-11 21:29:19 +02:00 committed by GitHub
parent 567d6ae8e7
commit 555be81026
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 12 deletions

View File

@ -0,0 +1,2 @@
Improve error handling in :c:func:`PyUnicode_BuildEncodingMap` where an
exception could end up being overwritten.

View File

@ -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 */