Skip to content

Commit 2c0792f

Browse files
committed
Fix memory leaks on failed realloc
1 parent 5f8d9d3 commit 2c0792f

3 files changed

Lines changed: 10 additions & 5 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix memory leaks in :func:`time.strftime` and :mod:`!_remote_debugging` when
2+
memory reallocation fails.

Modules/_remote_debugging/frames.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,14 @@ process_single_stack_chunk(
5656
return -1;
5757
}
5858

59-
this_chunk = PyMem_RawRealloc(this_chunk, actual_size);
60-
if (!this_chunk) {
59+
char *tmp = PyMem_RawRealloc(this_chunk, actual_size);
60+
if (!tmp) {
61+
PyMem_RawFree(this_chunk);
6162
PyErr_NoMemory();
6263
set_exception_cause(unwinder, PyExc_MemoryError, "Failed to reallocate stack chunk buffer");
6364
return -1;
6465
}
66+
this_chunk = tmp;
6567

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

Modules/timemodule.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -820,12 +820,13 @@ 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) {
826826
PyErr_NoMemory();
827827
return NULL;
828828
}
829+
*outbuf = tmp;
829830
#if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__)
830831
errno = 0;
831832
#endif

0 commit comments

Comments
 (0)