Skip to content

Commit ece5659

Browse files
miss-islingtonserhiy-storchaka
authored andcommitted
bpo-31626: Fixed a bug in debug memory allocator. (GH-3844) (#4191)
Removed a code that incorrectly detected in-place resizing in realloc() and wrote to freed memory. (cherry picked from commit b484d56)
1 parent f9a639b commit ece5659

File tree

2 files changed

+4
-11
lines changed

2 files changed

+4
-11
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fixed a bug in debug memory allocator. There was a write to freed memory
2+
after shrinking a memory block.

‎Objects/obmalloc.c‎

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1914,7 +1914,7 @@ static void *
19141914
_PyMem_DebugRawRealloc(void *ctx, void *p, size_t nbytes)
19151915
{
19161916
debug_alloc_api_t *api = (debug_alloc_api_t *)ctx;
1917-
uint8_t *q = (uint8_t *)p, *oldq;
1917+
uint8_t *q = (uint8_t *)p;
19181918
uint8_t *tail;
19191919
size_t total; /* nbytes + 4*SST */
19201920
size_t original_nbytes;
@@ -1931,20 +1931,11 @@ _PyMem_DebugRawRealloc(void *ctx, void *p, size_t nbytes)
19311931
/* overflow: can't represent total as a Py_ssize_t */
19321932
return NULL;
19331933

1934-
/* Resize and add decorations. We may get a new pointer here, in which
1935-
* case we didn't get the chance to mark the old memory with DEADBYTE,
1936-
* but we live with that.
1937-
*/
1938-
oldq = q;
1934+
/* Resize and add decorations. */
19391935
q = (uint8_t *)api->alloc.realloc(api->alloc.ctx, q - 2*SST, total);
19401936
if (q == NULL)
19411937
return NULL;
19421938

1943-
if (q == oldq && nbytes < original_nbytes) {
1944-
/* shrinking: mark old extra memory dead */
1945-
memset(q + nbytes, DEADBYTE, original_nbytes - nbytes);
1946-
}
1947-
19481939
write_size_t(q, nbytes);
19491940
assert(q[SST] == (uint8_t)api->api_id);
19501941
for (i = 1; i < SST; ++i)

0 commit comments

Comments
 (0)