From 17065c49fcce5a0b4d0894240902d1782a29857e Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Thu, 8 Jun 2023 09:24:49 +0200 Subject: [PATCH 1/3] gh-105375: Improve error handling in PyUnicode_BuildEncodingMap() --- Objects/unicodeobject.c | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index ffb4a87d4b9265..f3fc88e011ef13 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -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 */ From a4464137c4cc163f73d537d955e30a62cbc99d51 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Thu, 8 Jun 2023 09:25:57 +0200 Subject: [PATCH 2/3] Add NEWS --- .../2023-06-08-09-25-52.gh-issue-105375.ocB7fT.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2023-06-08-09-25-52.gh-issue-105375.ocB7fT.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-06-08-09-25-52.gh-issue-105375.ocB7fT.rst b/Misc/NEWS.d/next/Core and Builtins/2023-06-08-09-25-52.gh-issue-105375.ocB7fT.rst new file mode 100644 index 00000000000000..24fac2df4d0955 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2023-06-08-09-25-52.gh-issue-105375.ocB7fT.rst @@ -0,0 +1,2 @@ +Improve error handling in :c:func:`PyUnicode_BuildEncodingMap` where an +exception could end up being overwritten. From 94ae5bd7359a93219da3874bc5fe4c96d5fe4e7c Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Fri, 9 Jun 2023 21:52:05 +0200 Subject: [PATCH 3/3] Update Objects/unicodeobject.c --- Objects/unicodeobject.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index f3fc88e011ef13..38b72de88f3c0b 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -7937,7 +7937,8 @@ PyUnicode_BuildEncodingMap(PyObject* string) if (!result) return NULL; for (i = 0; i < length; i++) { - PyObject *key = PyLong_FromLong(PyUnicode_READ(kind, data, i)); + Py_UCS4 c = PyUnicode_READ(kind, data, i); + PyObject *key = PyLong_FromLong(c); if (key == NULL) { Py_DECREF(result); return NULL;