Skip to content

Commit e01a038

Browse files
authored
[3.14] gh-149931: Fix memory leaks on failed realloc (#150476)
(cherry picked from commit ec23ec6)
1 parent 730fc6a commit e01a038

2 files changed

Lines changed: 10 additions & 5 deletions

File tree

Modules/_remote_debugging_module.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2459,12 +2459,14 @@ process_single_stack_chunk(
24592459
return -1;
24602460
}
24612461

2462-
this_chunk = PyMem_RawRealloc(this_chunk, actual_size);
2463-
if (!this_chunk) {
2462+
char *tmp = PyMem_RawRealloc(this_chunk, actual_size);
2463+
if (!tmp) {
2464+
PyMem_RawFree(this_chunk);
24642465
PyErr_NoMemory();
24652466
set_exception_cause(unwinder, PyExc_MemoryError, "Failed to reallocate stack chunk buffer");
24662467
return -1;
24672468
}
2469+
this_chunk = tmp;
24682470

24692471
if (_Py_RemoteDebug_PagedReadRemoteMemory(&unwinder->handle, chunk_addr, actual_size, this_chunk) < 0) {
24702472
PyMem_RawFree(this_chunk);

Modules/timemodule.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -820,12 +820,15 @@ time_strftime1(time_char **outbuf, size_t *bufsize,
820820
PyErr_NoMemory();
821821
return NULL;
822822
}
823-
*outbuf = (time_char *)PyMem_Realloc(*outbuf,
824-
*bufsize*sizeof(time_char));
825-
if (*outbuf == NULL) {
823+
time_char *tmp = (time_char *)PyMem_Realloc(*outbuf,
824+
*bufsize*sizeof(time_char));
825+
if (tmp == NULL) {
826+
PyMem_Free(*outbuf);
827+
*outbuf = NULL;
826828
PyErr_NoMemory();
827829
return NULL;
828830
}
831+
*outbuf = tmp;
829832
#if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__)
830833
errno = 0;
831834
#endif

0 commit comments

Comments
 (0)