diff options
author | Victor Stinner <victor.stinner@gmail.com> | 2013-07-08 22:44:43 (GMT) |
---|---|---|
committer | Victor Stinner <victor.stinner@gmail.com> | 2013-07-08 22:44:43 (GMT) |
commit | c4266360fc70745d49b09f2c29cda91c1a007525 (patch) | |
tree | ff5dd727f232097d2b7c87dcf9095388f3b1c49b /Objects/obmalloc.c | |
parent | 9e6b4d715c46c0fa518fd76440b59516a9f6f7ea (diff) | |
download | cpython-c4266360fc70745d49b09f2c29cda91c1a007525.zip cpython-c4266360fc70745d49b09f2c29cda91c1a007525.tar.gz cpython-c4266360fc70745d49b09f2c29cda91c1a007525.tar.bz2 |
Issue #18408: Fix _PyMem_DebugRealloc()
Don't mark old extra memory dead before calling realloc(). realloc() can fail
and realloc() must not touch the original buffer on failure.
So mark old extra memory dead only on success if the new buffer did not move
(has the same address).
Diffstat (limited to 'Objects/obmalloc.c')
-rw-r--r-- | Objects/obmalloc.c | 14 |
1 files changed, 8 insertions, 6 deletions
diff --git a/Objects/obmalloc.c b/Objects/obmalloc.c index 8e25229..f146222 100644 --- a/Objects/obmalloc.c +++ b/Objects/obmalloc.c @@ -1780,7 +1780,7 @@ static void * _PyMem_DebugRealloc(void *ctx, void *p, size_t nbytes) { debug_alloc_api_t *api = (debug_alloc_api_t *)ctx; - uchar *q = (uchar *)p; + uchar *q = (uchar *)p, *oldq; uchar *tail; size_t total; /* nbytes + 4*SST */ size_t original_nbytes; @@ -1797,24 +1797,26 @@ _PyMem_DebugRealloc(void *ctx, void *p, size_t nbytes) /* overflow: can't represent total as a size_t */ return NULL; - if (nbytes < original_nbytes) { - /* shrinking: mark old extra memory dead */ - memset(q + nbytes, DEADBYTE, original_nbytes - nbytes + 2*SST); - } - /* Resize and add decorations. We may get a new pointer here, in which * case we didn't get the chance to mark the old memory with DEADBYTE, * but we live with that. */ + oldq = q; q = (uchar *)api->alloc.realloc(api->alloc.ctx, q - 2*SST, total); if (q == NULL) return NULL; + if (q == oldq && nbytes < original_nbytes) { + /* shrinking: mark old extra memory dead */ + memset(q + nbytes, DEADBYTE, original_nbytes - nbytes); + } + write_size_t(q, nbytes); assert(q[SST] == (uchar)api->api_id); for (i = 1; i < SST; ++i) assert(q[SST + i] == FORBIDDENBYTE); q += 2*SST; + tail = q + nbytes; memset(tail, FORBIDDENBYTE, SST); write_size_t(tail + SST, serialno); |