https://github.com/python/cpython/commit/c38d75ce321b72a67a04f41589a2e405ed3145a7
commit: c38d75ce321b72a67a04f41589a2e405ed3145a7
branch: main
author: Jelle Zijlstra <[email protected]>
committer: JelleZijlstra <[email protected]>
date: 2026-10-20T17:42:30Z
summary:

gh-159739: Preserve the normalization buffer on allocation failure (#159755)

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

Co-authored-by: Jelle Zijlstra <[email protected]>

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 60011f2ec84eb3..9bd3639d9c3f41 100644
--- a/Modules/unicodedata.c
+++ b/Modules/unicodedata.c
@@ -746,7 +746,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