Skip to content

Commit b484d56

Browse files
bpo-31626: Fixed a bug in debug memory allocator. (python#3844)
Removed a code that incorrectly detected in-place resizing in realloc() and wrote to freed memory.
1 parent b9052a0 commit b484d56

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
@@ -1460,7 +1460,7 @@ static void *
14601460
_PyMem_DebugRawRealloc(void *ctx, void *p, size_t nbytes)
14611461
{
14621462
debug_alloc_api_t *api = (debug_alloc_api_t *)ctx;
1463-
uint8_t *q = (uint8_t *)p, *oldq;
1463+
uint8_t *q = (uint8_t *)p;
14641464
uint8_t *tail;
14651465
size_t total; /* nbytes + 4*SST */
14661466
size_t original_nbytes;
@@ -1477,20 +1477,11 @@ _PyMem_DebugRawRealloc(void *ctx, void *p, size_t nbytes)
14771477
/* overflow: can't represent total as a Py_ssize_t */
14781478
return NULL;
14791479

1480-
/* Resize and add decorations. We may get a new pointer here, in which
1481-
* case we didn't get the chance to mark the old memory with DEADBYTE,
1482-
* but we live with that.
1483-
*/
1484-
oldq = q;
1480+
/* Resize and add decorations. */
14851481
q = (uint8_t *)api->alloc.realloc(api->alloc.ctx, q - 2*SST, total);
14861482
if (q == NULL)
14871483
return NULL;
14881484

1489-
if (q == oldq && nbytes < original_nbytes) {
1490-
/* shrinking: mark old extra memory dead */
1491-
memset(q + nbytes, DEADBYTE, original_nbytes - nbytes);
1492-
}
1493-
14941485
write_size_t(q, nbytes);
14951486
assert(q[SST] == (uint8_t)api->api_id);
14961487
for (i = 1; i < SST; ++i)

0 commit comments

Comments
 (0)