https://github.com/python/cpython/commit/77b293b57e92f2031fd572a4a01b9160a694a4f6
commit: 77b293b57e92f2031fd572a4a01b9160a694a4f6
branch: 3.15
author: Jelle Zijlstra <[email protected]>
committer: JelleZijlstra <[email protected]>
date: 2026-10-20T19:33:40Z
summary:

[3.15] gh-159739: Preserve the normalization buffer on allocation failure 
(GH-159755) (#159935)

Save the original buffer before calling PyMem_Resize(), which overwrites its 
first argument even on failure.

(cherry picked from commit c38d75ce321b72a67a04f41589a2e405ed3145a7)

files:
A Misc/NEWS.d/next/Library/2026-10-19-19-33-35.gh-issue-159739.b5fbca.rst
M Modules/unicodedata.c

diff --git 
a/Misc/NEWS.d/next/Library/2026-10-19-19-33-35.gh-issue-159739.b5fbca.rst 
b/Misc/NEWS.d/next/Library/2026-10-19-19-33-35.gh-issue-159739.b5fbca.rst
new file mode 100644
index 00000000000000..d4a662b10d9f4f
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2026-10-19-19-33-35.gh-issue-159739.b5fbca.rst
@@ -0,0 +1 @@
+Fix a memory leak when Unicode normalization cannot grow its temporary buffer.
diff --git a/Modules/unicodedata.c b/Modules/unicodedata.c
index 60df6821693813..b52801018b8846 100644
--- a/Modules/unicodedata.c
+++ b/Modules/unicodedata.c
@@ -762,9 +762,8 @@ nfd_nfkd(PyObject *self, PyObject *input, int k)
         }
 
         if (run_length > sortbuflen) {
-            Py_UCS4 *new_sortbuf = PyMem_Resize(sortbuf,
-                                                Py_UCS4,
-                                                run_length);
+            Py_UCS4 *new_sortbuf = sortbuf;
+            PyMem_Resize(new_sortbuf, Py_UCS4, run_length);
             if (new_sortbuf == NULL) {
                 PyErr_NoMemory();
                 PyMem_Free(sortbuf);

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to