From 2c0792f95d2734196b9e4a0e08a2b28698fecb73 Mon Sep 17 00:00:00 2001 From: AN Long Date: Sun, 17 May 2026 14:58:59 +0900 Subject: [PATCH 1/2] Fix memory leaks on failed realloc --- .../Library/2026-05-17-14-58-21.gh-issue-149931.9Oh2go.rst | 2 ++ Modules/_remote_debugging/frames.c | 6 ++++-- Modules/timemodule.c | 7 ++++--- 3 files changed, 10 insertions(+), 5 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2026-05-17-14-58-21.gh-issue-149931.9Oh2go.rst diff --git a/Misc/NEWS.d/next/Library/2026-05-17-14-58-21.gh-issue-149931.9Oh2go.rst b/Misc/NEWS.d/next/Library/2026-05-17-14-58-21.gh-issue-149931.9Oh2go.rst new file mode 100644 index 00000000000000..5753e0b51c1ceb --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-05-17-14-58-21.gh-issue-149931.9Oh2go.rst @@ -0,0 +1,2 @@ +Fix memory leaks in :func:`time.strftime` and :mod:`!_remote_debugging` when +memory reallocation fails. diff --git a/Modules/_remote_debugging/frames.c b/Modules/_remote_debugging/frames.c index bbdfce3f7201d9..edca59f3f5fc13 100644 --- a/Modules/_remote_debugging/frames.c +++ b/Modules/_remote_debugging/frames.c @@ -56,12 +56,14 @@ process_single_stack_chunk( return -1; } - this_chunk = PyMem_RawRealloc(this_chunk, actual_size); - if (!this_chunk) { + char *tmp = PyMem_RawRealloc(this_chunk, actual_size); + if (!tmp) { + PyMem_RawFree(this_chunk); PyErr_NoMemory(); set_exception_cause(unwinder, PyExc_MemoryError, "Failed to reallocate stack chunk buffer"); return -1; } + this_chunk = tmp; if (_Py_RemoteDebug_PagedReadRemoteMemory(&unwinder->handle, chunk_addr, actual_size, this_chunk) < 0) { PyMem_RawFree(this_chunk); diff --git a/Modules/timemodule.c b/Modules/timemodule.c index 25e744d7da25c7..26e54451bf3ed1 100644 --- a/Modules/timemodule.c +++ b/Modules/timemodule.c @@ -820,12 +820,13 @@ time_strftime1(time_char **outbuf, size_t *bufsize, PyErr_NoMemory(); return NULL; } - *outbuf = (time_char *)PyMem_Realloc(*outbuf, - *bufsize*sizeof(time_char)); - if (*outbuf == NULL) { + time_char *tmp = (time_char *)PyMem_Realloc(*outbuf, + *bufsize*sizeof(time_char)); + if (tmp == NULL) { PyErr_NoMemory(); return NULL; } + *outbuf = tmp; #if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__) errno = 0; #endif From eab61bf01efa6fe114eeaaf7d2d121e7e6e0362a Mon Sep 17 00:00:00 2001 From: AN Long Date: Sun, 17 May 2026 21:21:43 +0900 Subject: [PATCH 2/2] Remove news entry --- .../next/Library/2026-05-17-14-58-21.gh-issue-149931.9Oh2go.rst | 2 -- 1 file changed, 2 deletions(-) delete mode 100644 Misc/NEWS.d/next/Library/2026-05-17-14-58-21.gh-issue-149931.9Oh2go.rst diff --git a/Misc/NEWS.d/next/Library/2026-05-17-14-58-21.gh-issue-149931.9Oh2go.rst b/Misc/NEWS.d/next/Library/2026-05-17-14-58-21.gh-issue-149931.9Oh2go.rst deleted file mode 100644 index 5753e0b51c1ceb..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-05-17-14-58-21.gh-issue-149931.9Oh2go.rst +++ /dev/null @@ -1,2 +0,0 @@ -Fix memory leaks in :func:`time.strftime` and :mod:`!_remote_debugging` when -memory reallocation fails.