summaryrefslogtreecommitdiff
path: root/py/gc.c
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2016-02-01 21:32:05 +0000
committerDamien George <damien.p.george@gmail.com>2016-04-18 15:09:34 +0100
commit1c0343f9d991c241d335712593f3a63858dc91b6 (patch)
tree2f2757a37edaffcfbf448a6acb0e1bbe524f525c /py/gc.c
parent2d9531a777e01c982ce8f2e5867b05bbac74373c (diff)
py/gc: Zero out all newly allocated memory to prevent stale pointers.
Diffstat (limited to 'py/gc.c')
-rw-r--r--py/gc.c8
1 files changed, 4 insertions, 4 deletions
diff --git a/py/gc.c b/py/gc.c
index 4e4cd9f30..41526c8b0 100644
--- a/py/gc.c
+++ b/py/gc.c
@@ -409,12 +409,12 @@ found:
void *ret_ptr = (void*)(MP_STATE_MEM(gc_pool_start) + start_block * BYTES_PER_BLOCK);
DEBUG_printf("gc_alloc(%p)\n", ret_ptr);
- // zero out the additional bytes of the newly allocated blocks
+ // Zero out all the bytes of the newly allocated blocks.
// This is needed because the blocks may have previously held pointers
// to the heap and will not be set to something else if the caller
// doesn't actually use the entire block. As such they will continue
// to point to the heap and may prevent other blocks from being reclaimed.
- memset((byte*)ret_ptr + n_bytes, 0, (end_block - start_block + 1) * BYTES_PER_BLOCK - n_bytes);
+ memset((byte*)ret_ptr, 0, (end_block - start_block + 1) * BYTES_PER_BLOCK);
#if MICROPY_ENABLE_FINALISER
if (has_finaliser) {
@@ -620,8 +620,8 @@ void *gc_realloc(void *ptr_in, size_t n_bytes, bool allow_move) {
ATB_FREE_TO_TAIL(bl);
}
- // zero out the additional bytes of the newly allocated blocks (see comment above in gc_alloc)
- memset((byte*)ptr_in + n_bytes, 0, new_blocks * BYTES_PER_BLOCK - n_bytes);
+ // zero out the bytes of the newly allocated blocks (see comment above in gc_alloc)
+ memset((byte*)ptr_in + n_blocks * BYTES_PER_BLOCK, 0, (new_blocks - n_blocks) * BYTES_PER_BLOCK);
#if EXTENSIVE_HEAP_PROFILING
gc_dump_alloc_table();