From: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> Date: Tue, 26 May 2026 01:04:49 +0000 (+0200) Subject: [3.15] gh-149931: Fix memory leaks on failed realloc (GH-149932) (#150439) X-Git-Tag: v3.15.0b2~59 X-Git-Url: http://git.ipfire.org/gitweb/index.cgi?a=commitdiff_plain;h=d73e43317e08e6d2ce693c61ca6e751996da2d43;p=thirdparty%2FPython%2Fcpython.git [3.15] gh-149931: Fix memory leaks on failed realloc (GH-149932) (#150439) --- diff --git a/Modules/_remote_debugging/frames.c b/Modules/_remote_debugging/frames.c index 8d8019396b3e..d73cd080dc47 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 25e744d7da25..d90bf1f2ef90 100644 --- a/Modules/timemodule.c +++ b/Modules/timemodule.c @@ -820,12 +820,15 @@ 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) { + PyMem_Free(*outbuf); + *outbuf = NULL; PyErr_NoMemory(); return NULL; } + *outbuf = tmp; #if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__) errno = 0; #endif