From d4d9719258db8db0c273bd779c945772e20a54c3 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Tue, 20 Oct 2026 10:56:34 -0700 Subject: [PATCH] [3.14] gh-159739: Preserve the normalization buffer on allocation failure (GH-159755) Save the original buffer before calling PyMem_Resize(), which overwrites its first argument even on failure. (cherry picked from commit c38d75ce321b72a67a04f41589a2e405ed3145a7) --- .../Library/2026-10-19-19-33-35.gh-issue-159739.b5fbca.rst | 1 + Modules/unicodedata.c | 5 ++--- 2 files changed, 3 insertions(+), 3 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2026-10-19-19-33-35.gh-issue-159739.b5fbca.rst 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 d9b477a12b4b47..014990ac60991e 100644 --- a/Modules/unicodedata.c +++ b/Modules/unicodedata.c @@ -716,9 +716,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);